-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Click to insert on empty render prop * fix(editor) enable selection and editing of render props * Nested navigator view * wip - popup * wip - select prop from variants * max width on popup * Quick fix for SpyWrapper issue * Fix non-rendered elements * Turn on FS * don't consider elements within generated * add important todo * remove element from elementsWithin * Only allow insert on empty render prop * whoops * revert deleting generated elements * Added label for children * Updated design * Empty render prop slot * use context menu styles in render prop picker * Fix children label when there are only empty props * Better fake uids * Remove deletion stuff * Remove childOrAttribute * Updated design * Make expression check safer * Cleanup * Adapt jsx elements in props to render prop navigator * Fix children label * Fix children label * remove `isRendered` * wip - test * test with render prop filled out * test with empty render prop * cleanup * remove unused import * add early return * `notRenderPropChildren` * `isEntryAPlaceholder` * remove `SyntheticNavigatorEntry.renderProp` * remove renderprop too * show slot for null value * slot navigator entry * popup * move render prop picker to its own file --------- Co-authored-by: Balint Gabor <[email protected]> Co-authored-by: RheeseyB <[email protected]>
- Loading branch information
1 parent
0150d8c
commit c4dda31
Showing
7 changed files
with
156 additions
and
12 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
121 changes: 121 additions & 0 deletions
121
editor/src/components/navigator/navigator-item/render-prop-selector-popup.tsx
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,121 @@ | ||
import React from 'react' | ||
import { useContextMenu, Menu } from 'react-contexify' | ||
import { MetadataUtils } from '../../../core/model/element-metadata-utils' | ||
import { | ||
getJSXElementNameAsString, | ||
jsExpressionOtherJavaScriptSimple, | ||
} from '../../../core/shared/element-template' | ||
import type { ElementPath } from '../../../core/shared/project-file-types' | ||
import { useDispatch } from '../../editor/store/dispatch-context' | ||
import { Substores, useEditorState } from '../../editor/store/store-hook' | ||
import { setProp_UNSAFE } from '../../editor/actions/action-creators' | ||
import * as EP from '../../../core/shared/element-path' | ||
import * as PP from '../../../core/shared/property-path' | ||
|
||
const usePreferredChildrenForTargetProp = (target: ElementPath, prop: string) => { | ||
const selectedJSXElement = useEditorState( | ||
Substores.metadata, | ||
(store) => MetadataUtils.getJSXElementFromMetadata(store.editor.jsxMetadata, target), | ||
'usePreferredChildrenForSelectedElement selectedJSXElement', | ||
) | ||
|
||
const preferredChildrenForTargetProp = useEditorState( | ||
Substores.restOfEditor, | ||
(store) => { | ||
if (selectedJSXElement == null) { | ||
return null | ||
} | ||
|
||
const targetName = getJSXElementNameAsString(selectedJSXElement.name) | ||
// TODO: we don't deal with components registered with the same name in multiple files | ||
for (const file of Object.values(store.editor.propertyControlsInfo)) { | ||
for (const [name, value] of Object.entries(file)) { | ||
if (name === targetName) { | ||
for (const [registeredPropName, registeredPropValue] of Object.entries( | ||
value.properties, | ||
)) { | ||
if ( | ||
registeredPropName === prop && | ||
registeredPropValue.control === 'jsx' && | ||
registeredPropValue.preferredChildComponents != null | ||
) { | ||
return registeredPropValue.preferredChildComponents | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null | ||
}, | ||
'usePreferredChildrenForSelectedElement propertyControlsInfo', | ||
) | ||
|
||
if (selectedJSXElement == null || preferredChildrenForTargetProp == null) { | ||
return null | ||
} | ||
|
||
return preferredChildrenForTargetProp | ||
} | ||
|
||
export const useShowRenderPropPicker = (id: string) => { | ||
const { show, hideAll } = useContextMenu({ id }) | ||
const onClick = React.useCallback( | ||
(event: React.MouseEvent<HTMLDivElement>) => { | ||
show(event) | ||
}, | ||
[show], | ||
) | ||
|
||
return { showRenderPropPicker: onClick, hideRenderPropPicker: hideAll } | ||
} | ||
|
||
interface RenderPropPickerProps { | ||
target: ElementPath | ||
prop: string | ||
key: string | ||
id: string | ||
} | ||
|
||
export const RenderPropPicker = React.memo<RenderPropPickerProps>(({ key, id, target, prop }) => { | ||
const preferredChildrenForTargetProp = usePreferredChildrenForTargetProp( | ||
EP.parentPath(target), | ||
prop, | ||
) | ||
|
||
const dispatch = useDispatch() | ||
|
||
const onItemClick = React.useCallback( | ||
(rawJSCodeForRenderProp: string) => (e: React.MouseEvent) => { | ||
e.stopPropagation() | ||
e.preventDefault() | ||
|
||
dispatch([ | ||
setProp_UNSAFE( | ||
EP.parentPath(target), | ||
PP.create(prop), | ||
jsExpressionOtherJavaScriptSimple(rawJSCodeForRenderProp, []), | ||
), | ||
]) | ||
}, | ||
[dispatch, prop, target], | ||
) | ||
|
||
if (preferredChildrenForTargetProp == null) { | ||
return null | ||
} | ||
|
||
const rawJSToInsert: Array<string> = (preferredChildrenForTargetProp ?? []).flatMap((data) => | ||
data.variants == null ? `<${data.name} />` : data.variants.map((v) => v.code), | ||
) | ||
|
||
return ( | ||
<Menu key={key} id={id} animation={false} style={{ padding: 8 }}> | ||
{rawJSToInsert.map((option, idx) => ( | ||
<div key={idx} onClick={onItemClick(option)}> | ||
{option} | ||
</div> | ||
))} | ||
</Menu> | ||
) | ||
}) |
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