Skip to content

Commit 48b26bb

Browse files
authored
Merge pull request #596 from components-ai/text-styles
Apply styles to text parent
2 parents a2d8413 + 312acb3 commit 48b26bb

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

.changeset/pretty-comics-rush.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+
Apply styles to text parent

packages/gui/src/components/html/Editor.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useTheme } from '../providers/ThemeContext'
88
import { NodeEditor } from './Editors/NodeEditor'
99
import { TreeNode } from './TreeNode'
1010
import { Import } from './Import'
11+
import { isText } from '../../lib/codegen/util'
1112

1213
const TABS_TRIGGER_STYLES: any = {
1314
all: 'unset',
@@ -61,6 +62,13 @@ export function HtmlEditor() {
6162
const selected = providedSelected || []
6263
const nodeValue = getChildAtPath(value, selected)
6364

65+
let nodeForStyleEditor = nodeValue
66+
const stylePath = [...selected]
67+
if (isText(nodeValue)) {
68+
stylePath.pop()
69+
nodeForStyleEditor = getChildAtPath(value, stylePath)
70+
}
71+
6472
return (
6573
<div
6674
sx={{
@@ -112,10 +120,10 @@ export function HtmlEditor() {
112120
<Editor
113121
key={selected.join('-')}
114122
theme={theme}
115-
styles={nodeValue.style ?? {}}
123+
styles={nodeForStyleEditor.style ?? {}}
116124
onChange={(newStyles) => {
117-
const newItem = { ...nodeValue, style: newStyles }
118-
onChange(setChildAtPath(value, selected, newItem))
125+
const newItem = { ...nodeForStyleEditor, style: newStyles }
126+
onChange(setChildAtPath(value, stylePath, newItem))
119127
}}
120128
showRegenerate
121129
showAddProperties

packages/gui/src/components/html/util.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ export function getChildAtPath(element: HtmlNode, path: ElementPath): HtmlNode {
4545
return getChildAtPath(child, rest)
4646
}
4747

48+
export function getParentAtPath(
49+
element: HtmlNode,
50+
path: ElementPath
51+
): HtmlNode {
52+
const newPath = [...path]
53+
newPath.pop()
54+
return getChildAtPath(element, newPath)
55+
}
56+
4857
export function addChildAtPath(
4958
element: HtmlNode,
5059
path: ElementPath,

0 commit comments

Comments
 (0)