Skip to content

Commit f5974f3

Browse files
authored
Merge pull request #507 from components-ai/nested-component-selection
Improve nested component selection
2 parents 168ab9a + 474882f commit f5974f3

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

.changeset/cyan-mails-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compai/css-gui': patch
3+
---
4+
5+
Improve nested component selection

apps/docs/data/initial-html-editor-data.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,4 +1599,23 @@ export const initialComponents: any = [
15991599
children: [{ type: 'slot', name: 'children', value: 'CSS.GUI' }],
16001600
},
16011601
},
1602+
{
1603+
type: 'component',
1604+
id: 'ddeeff',
1605+
tagName: 'Footer',
1606+
value: {
1607+
tagName: 'footer',
1608+
attributes: {},
1609+
style: {},
1610+
children: [
1611+
{
1612+
type: 'element',
1613+
tagName: 'h1',
1614+
attributes: {},
1615+
style: {},
1616+
children: [{ type: 'text', value: 'A footer!!!!' }],
1617+
},
1618+
],
1619+
},
1620+
},
16021621
]

packages/gui/src/components/html/Component/Provider.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { createContext, ReactNode, useContext } from 'react'
2+
import { useHtmlEditor } from '../Provider'
23
import { ComponentData, ElementPath } from '../types'
34

45
const DEFAULT_COMPONENT_VALUE = {}
56

67
type ComponentProviderType = {
78
value?: ComponentData
89
path?: ElementPath
10+
selectComponent?(e: MouseEvent): void
911
}
1012

1113
export function useComponent() {
@@ -28,8 +30,13 @@ export function ComponentProvider({
2830
path,
2931
children,
3032
}: ComponentProviderProps) {
33+
const { setSelected } = useHtmlEditor()
34+
const selectComponent = (e: MouseEvent) => {
35+
setSelected(path)
36+
e.stopPropagation()
37+
}
3138
return (
32-
<ComponentContext.Provider value={{ value, path }}>
39+
<ComponentContext.Provider value={{ value, path, selectComponent }}>
3340
{children}
3441
</ComponentContext.Provider>
3542
)

packages/gui/src/components/html/Renderer/Element.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ export function ElementRenderer({
1111
path,
1212
...canvasElementProps
1313
}: CanvasElementProps) {
14-
const props = useCanvasProps({ value, path, ...canvasElementProps })
14+
const { selectComponent } = useComponent()
15+
const { onClick, ...props } = useCanvasProps({
16+
value,
17+
path,
18+
...canvasElementProps,
19+
})
1520

1621
if (value.type === 'slot') {
1722
return <SlotRenderer value={value} path={path} />
@@ -21,12 +26,20 @@ export function ElementRenderer({
2126

2227
const Tag: any = value.tagName || 'div'
2328

29+
const handleClick = (e: MouseEvent) => {
30+
if (selectComponent) {
31+
return selectComponent(e)
32+
}
33+
34+
onClick(e)
35+
}
36+
2437
if (isVoidElement(Tag)) {
25-
return <Tag {...props} />
38+
return <Tag {...props} onClick={handleClick} />
2639
}
2740

2841
return (
29-
<Tag {...props}>
42+
<Tag {...props} onClick={handleClick}>
3043
<ChildrenRenderer value={value.children} path={path} />
3144
</Tag>
3245
)
@@ -56,15 +69,14 @@ interface ComponentRendererProps {
5669
path: ElementPath
5770
}
5871
export function ComponentRenderer({ value, path }: ComponentRendererProps) {
59-
const { onClick } = useCanvasProps({ value, path })
6072
const fullValue = {
6173
...value.value,
6274
attributes: mergeComponentAttributes(value),
6375
}
6476

6577
return (
6678
<ComponentProvider value={value} path={path}>
67-
<ElementRenderer value={fullValue} path={path} onClick={onClick} />
79+
<ElementRenderer value={fullValue} path={path} />
6880
</ComponentProvider>
6981
)
7082
}

0 commit comments

Comments
 (0)