You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/pages/docs/guides/components/specs.mdx
+50-60Lines changed: 50 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,11 +13,13 @@ Specs are JavaScript modules that define a component's API. They enable all thre
13
13
14
14
## Using Specs
15
15
16
-
Specs are more verbose than [`settings`](/docs/guides/components/settings) and allow additional metadata like natural text description, code examples and metadata like [`usageLevel`](#usage-levels) to help hint AI and other humans on how to use your components.
16
+
Specs are more opinionated version of [`settings`](/docs/guides/components/settings) particularly created to be a single source of truth for design frameworks.
17
+
18
+
Specs allow additional metadata like natural text description, code examples and metadata like [`usageLevel`](#usage-levels) to be included with your components to help hint AI and other humans on how to use your components.
> First-party components like those in [UI Primitives](/ui/primitives/button) generate their documentation programatically using [`SpecReader`](/docs/api/specs/spec-reader). The types, states, and variations you see documented are read directly from `.spec.js` files—not manually written.
36
39
37
40
38
41
## Inside Components
39
42
40
-
### Settings
43
+
### Automatic Settings
41
44
42
-
Spec attributes become reactive settings available in templates and lifecycle callbacks:
45
+
Specs automatically generate [`defaultSettings`](docs/guides/components/settings) for your component. These settings can be used like other settings and read inside your component or template.
43
46
44
-
```javascript
45
-
// In your spec
46
-
variations: [
47
+
```javascript title='component.spec.js'
48
+
content: [
47
49
{
48
-
name:'Emphasis',
49
-
attribute:'emphasis',
50
-
options: [
51
-
{ name:'Primary', value:'primary' },
52
-
{ name:'Secondary', value:'secondary' },
53
-
],
50
+
name:'Icon',
51
+
attribute:'icon',
52
+
description:'include an icon',
54
53
},
55
54
]
56
55
```
57
56
58
-
```html
59
-
<!-- In your HTML -->
60
-
<ui-buttonprimarylarge>Save</ui-button>
57
+
```html title='page.html'
58
+
<ui-buttonicon="save">Save</ui-button>
61
59
```
62
60
63
-
```sui
64
-
<!-- In your template -->
65
-
<button class="ui button {ui}">
66
-
Current emphasis: {emphasis}
67
-
Current size: {size}
68
-
</button>
61
+
```sui title='component.html'
62
+
{#if icon}
63
+
<ui-icon icon={icon}>
64
+
{/if}
69
65
```
70
66
71
-
The spec's `optionAttributes` maps `primary` → `emphasis="primary"`, making the attribute available as <code>{emphasis}</code> in templates.
72
-
73
-
### The {ui} CSS Class String
74
-
75
-
Spec attributes automatically generate CSS classes through the <code>{ui}</code> variable:
76
-
77
-
```sui
78
-
<div class="ui button {ui}">
79
-
<!-- {ui} becomes "primary large" -->
80
-
</div>
81
-
```
82
67
83
-
The transformation rules:
84
-
-**Option values**: `emphasis="primary"` → class `"primary"`
85
-
-**Boolean attributes**: `disabled` → class `"disabled"`
86
-
-**Attribute classes**: `icon="save"` → class `"icon"` (if in spec's attributeClasses)
68
+
### Styling Classes
87
69
88
-
### Accessing Settings in Lifecycle
70
+
Specs also create a stringified styling class in your [data context](docs/guides/components/rendering)`{ui}` for `types` and `variations` parts of your spec that are related to styling, regardless of how that data is specified.
89
71
90
-
Settings are available as destructured arguments in all [lifecycle callbacks](/docs/guides/components/lifecycle):
72
+
When `includeAttributeClass: true` is set on a variation, the attribute name is also included as a class. This allows styling all options of a variation together:
91
73
92
-
```javascript
93
-
defineComponent({
94
-
tagName:'ui-badge',
95
-
componentSpec,
96
-
97
-
createComponent: ({ settings }) => ({
98
-
setStatus(newStatus) {
99
-
settings.status= newStatus; // Updates setting and triggers re-render
100
-
},
74
+
```javascript title='component.spec.js'
75
+
variations: [
76
+
{
77
+
name:'Colored',
78
+
attribute:'color',
79
+
includeAttributeClass:true, // adds 'color' class
80
+
options: [
81
+
{ name:'Red', value:'red' },
82
+
{ name:'Blue', value:'blue' },
83
+
],
84
+
},
85
+
]
86
+
```
101
87
102
-
getCurrentStatus() {
103
-
returnsettings.status;
104
-
},
105
-
}),
88
+
```html title='page.html'
89
+
<ui-buttonicon="save"red>Save</ui-button>
90
+
```
106
91
107
-
onCreated: ({ settings, el }) => {
108
-
console.log('Initial status:', settings.status);
109
-
},
110
-
});
92
+
```sui title='component.html'
93
+
<!-- ui is 'color red' !-->
94
+
<div class="{ui}button">
95
+
{#if icon}
96
+
<ui-icon icon={icon}>
97
+
{/if}
98
+
{text}
99
+
</div>
111
100
```
112
101
102
+
113
103
## Spec Parts
114
104
115
105
Every spec has these top-level fields:
@@ -175,7 +165,7 @@ types: [
175
165
176
166
### States
177
167
178
-
Define how you component varies over time
168
+
Define how your component varies over time
179
169
180
170
```javascript
181
171
states: [
@@ -189,7 +179,7 @@ states: [
189
179
190
180
### Variations
191
181
192
-
Define stackable visual modifications:
182
+
Define stackable visual modifications
193
183
194
184
```javascript
195
185
variations: [
@@ -212,7 +202,7 @@ variations: [
212
202
213
203
### Settings
214
204
215
-
Define settings that modify how your component functions:
205
+
Define settings that modify how your component functions
0 commit comments