-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic API for custom components (#2)
## Description This PR adds a simple API for creating custom components: - components with type `Custom` are treated like `Stack` when measured, and its children are positioned at the top-start. - `Custom` function accepts additional argument with custom config - custom config may be used to store data between updates - custom config may additionally contain hooks for specific moments: - `dropHandler` - invoked when the node is dropped from the tree - `createView` - invoked when the view enters the view hierarchy, when implemented it must return a reference to the created view - `overrideViewProps` - invoked after the ViewManager finishes setting up the newly created view, it may be used to overwrite them - `updateView` - invoked during update, receives previous and the next node - `deleteView` - invoked when the view leaves the view hierarchy, when implemented it must delete the created view (note that this method may be called without calling `dropHandler` to reorder views)
- Loading branch information
1 parent
b28f3df
commit beaa23c
Showing
8 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { RequireSome } from '../../utils.js' | ||
import { ViewNode } from '../ViewNode.js' | ||
import { WorkingTree } from '../WorkingTree.js' | ||
import { NodeType } from '../../NodeType.js' | ||
import { ConfigBuilder } from '../props/Config.js' | ||
import { RenderNode } from '../../renderer/Renderer.js' | ||
|
||
export interface CustomViewProps extends Record<string, unknown> { | ||
/** | ||
* invoked when the node is dropped from the tree | ||
*/ | ||
dropHandler?: () => void | ||
|
||
/** | ||
* invoked when the view enters the view hierarchy, when implemented itmust return a | ||
* reference to the created view | ||
*/ | ||
createView?: (config: RenderNode) => unknown | ||
|
||
/** | ||
* invoked after the ViewManager finishes setting up the newly created view, it may | ||
* be used to overwrite them | ||
*/ | ||
overrideViewProps?: (config: RenderNode) => void | ||
|
||
/** | ||
* invoked during update, receives previous and the next node | ||
*/ | ||
updateView?: (previous: RenderNode, next: RenderNode) => void | ||
|
||
/** | ||
* invoked when the view leaves the view hierarchy, when implemented it must delete the created | ||
* view (note that this method may be called without calling dropHandler to reorder views) | ||
*/ | ||
deleteView?: (config: RenderNode) => void | ||
} | ||
|
||
export function Custom( | ||
configBuilder: RequireSome<ConfigBuilder, 'build'>, | ||
customViewProps: CustomViewProps, | ||
body: () => void | ||
) { | ||
const config = configBuilder.build() | ||
const current = WorkingTree.current as ViewNode | ||
|
||
const context = current.create( | ||
{ | ||
id: config.id, | ||
type: NodeType.Custom, | ||
config: config, | ||
body: body, | ||
}, | ||
customViewProps | ||
) | ||
|
||
current.children.push(context) | ||
|
||
WorkingTree.withContext(context, body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters