Skip to content

Commit bcbfaf8

Browse files
committed
feat(render): add more CSS properties to Style and apply in renderer 🎨
- Apply color, margin, opacity, padding, borderRadius, boxShadow, transition in applyStyle - Simplify arrow params in Create and update JSDoc - Extend Style type with same properties
1 parent 5a9cd5b commit bcbfaf8

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

USAGE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Allowed keys are exactly the above. Extra keys cause validation to throw.
6969
## Layout and Style
7070

7171
- **Layout**`width`, `height`, `x`, `y`, `flex`, `gap`. Number values are converted to `Npx`; strings (e.g. `'100%'`, `'auto'`) are used as-is. Applied to the element’s `style` (width, height, left, top, flex, gap). Setting `flex` or `gap` may set `display` to `block` or `flex` when needed.
72-
- **Style**`fill``backgroundColor`, `stroke``border`, `font``font`, `border``border`. All string values.
72+
- **Style**All string values, mapped to `element.style` (CSSStyleProperties). Semantic: `fill``backgroundColor`, `stroke``border`. Direct: `font`, `border`, `color`, `padding`, `margin`, `borderRadius`, `boxShadow`, `opacity`, `transition`.
7373

7474
## Void Tags and Special Elements
7575

@@ -103,7 +103,7 @@ Types are re-exported from the package: `import type { Attrs, Definition, Layout
103103
- **Schema:** `{ readonly root: readonly Node[] }` — Output of `create`; frozen.
104104
- **Node:** Object with `type` (string) and optional `id`, `layout`, `style`, `attrs`, `content`, `src`, `alt`, `children` (readonly array of Node). Void tags must not have `children`.
105105
- **Layout:** Optional `width`, `height`, `x`, `y`, `flex`, `gap` — each `number | string`.
106-
- **Style:** Optional `fill`, `stroke`, `font`, `border` — each `string`.
106+
- **Style:** Optional `border`, `borderRadius`, `boxShadow`, `color`, `fill`, `font`, `margin`, `opacity`, `padding`, `stroke`, `transition` — each `string`.
107107
- **Attrs:** `Record<string, string | number | boolean>` — HTML attributes. Special handling for `class`/`className`, `style` (string), `value`, `checked`, `disabled` on form elements.
108108

109109
## Reference

src/Create.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ export default class Create {
1414
*/
1515
static create(definition: Types.Definition | unknown): Types.Schema {
1616
const validated = Validator.validateDefinition(definition)
17-
const frozenRoot = validated.root.map((schemaNode) => Create.freezeNode(schemaNode))
17+
const frozenRoot = validated.root.map(schemaNode => Create.freezeNode(schemaNode))
1818
Object.freeze(frozenRoot)
1919
return Object.freeze({ root: frozenRoot })
2020
}
21+
2122
/**
2223
* Deep-freeze a single node and its children.
2324
* @description Freezes layout, style, attrs, and children recursively.
@@ -35,7 +36,7 @@ export default class Create {
3536
Object.freeze(node.attrs)
3637
}
3738
if (node.children) {
38-
const frozenChildren = node.children.map((childNode) => Create.freezeNode(childNode))
39+
const frozenChildren = node.children.map(childNode => Create.freezeNode(childNode))
3940
Object.freeze(frozenChildren)
4041
return Object.freeze({ ...node, children: frozenChildren }) as Readonly<Types.Node>
4142
}

src/Render.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ export default class Render {
9797
}
9898

9999
/**
100-
* Apply style to element (fill, stroke, font, border).
101-
* @description Maps schema style to CSS properties.
100+
* Apply schema style to element.
101+
* @description Maps style keys to element inline CSS.
102102
* @param element - Target HTML element
103-
* @param style - Style object
103+
* @param style - Schema style object
104104
*/
105105
static applyStyle(element: HTMLElement, style: Types.Style): void {
106106
const elementStyle = element.style
@@ -116,6 +116,27 @@ export default class Render {
116116
if (style.border) {
117117
elementStyle.border = style.border
118118
}
119+
if (style.color) {
120+
elementStyle.color = style.color
121+
}
122+
if (style.padding) {
123+
elementStyle.padding = style.padding
124+
}
125+
if (style.margin) {
126+
elementStyle.margin = style.margin
127+
}
128+
if (style.borderRadius) {
129+
elementStyle.borderRadius = style.borderRadius
130+
}
131+
if (style.boxShadow) {
132+
elementStyle.boxShadow = style.boxShadow
133+
}
134+
if (style.opacity) {
135+
elementStyle.opacity = style.opacity
136+
}
137+
if (style.transition) {
138+
elementStyle.transition = style.transition
139+
}
119140
}
120141

121142
/**

src/Types.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,31 @@ export type Schema2UIDefault = {
7676

7777
/**
7878
* Visual style for a node.
79-
* @description Fill, stroke, font, border mapped to CSS.
79+
* @description Fill, stroke, font, border plus CSS properties.
8080
*/
8181
export type Style = {
82-
/** Border shorthand */
82+
/** Border shorthand (CSS border) */
8383
border?: string
84-
/** Background or fill color */
84+
/** Border radius (CSS border-radius) */
85+
borderRadius?: string
86+
/** Box shadow (CSS box-shadow) */
87+
boxShadow?: string
88+
/** Text/foreground color (CSS color) */
89+
color?: string
90+
/** Background or fill color (→ backgroundColor) */
8591
fill?: string
86-
/** Font shorthand */
92+
/** Font shorthand (CSS font) */
8793
font?: string
88-
/** Border or stroke */
94+
/** Margin (CSS margin) */
95+
margin?: string
96+
/** Opacity (CSS opacity) */
97+
opacity?: string
98+
/** Padding (CSS padding) */
99+
padding?: string
100+
/** Border or stroke (→ border) */
89101
stroke?: string
102+
/** Transition (CSS transition) */
103+
transition?: string
90104
}
91105

92106
/** Plain object record for validation. */

0 commit comments

Comments
 (0)