Skip to content

Commit c80c28c

Browse files
committed
Docs: Continue fixing spec docs
1 parent 009bf00 commit c80c28c

File tree

1 file changed

+50
-60
lines changed
  • docs/src/pages/docs/guides/components

1 file changed

+50
-60
lines changed

docs/src/pages/docs/guides/components/specs.mdx

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ Specs are JavaScript modules that define a component's API. They enable all thre
1313

1414
## Using Specs
1515

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.
1719

1820
<PlaygroundExample id="component-specs" direction="horizontal"></PlaygroundExample>
1921

20-
Specs are used as a single source of truth to power your component and its documentation
22+
Specs are used as a single source of truth to power your component and its documentation:
2123
- Translate between multiple supported syntaxes for using your web component
2224
- Generates CSS classes through the [<code>&#123;ui&#125;</code> template variable](#the-ui-css-class-string)
2325
- Creates documentation using [`getDefinition()`](/docs/api/specs/documentation#getdefinition)
@@ -30,86 +32,74 @@ Specs are used as a single source of truth to power your component and its docum
3032
| **Component Type** | Design system | One-off component |
3133
| **Attributes** | Support shorthand | HTML attributes only |
3234
| **CSS Classes** | Auto-generated | Manual |
35+
| **Definition** | Portable JSON | JavaScript code |
3336
| **Documentation** | Auto-generated | Manual |
3437

3538
> 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.
3639
3740

3841
## Inside Components
3942

40-
### Settings
43+
### Automatic Settings
4144

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.
4346

44-
```javascript
45-
// In your spec
46-
variations: [
47+
```javascript title='component.spec.js'
48+
content: [
4749
{
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',
5453
},
5554
]
5655
```
5756

58-
```html
59-
<!-- In your HTML -->
60-
<ui-button primary large>Save</ui-button>
57+
```html title='page.html'
58+
<ui-button icon="save">Save</ui-button>
6159
```
6260

63-
```sui
64-
<!-- In your template -->
65-
<button class="ui button &#123;ui&#125;">
66-
Current emphasis: &#123;emphasis&#125;
67-
Current size: &#123;size&#125;
68-
</button>
61+
```sui title='component.html'
62+
{#if icon}
63+
<ui-icon icon={icon}>
64+
{/if}
6965
```
7066

71-
The spec's `optionAttributes` maps `primary``emphasis="primary"`, making the attribute available as <code>&#123;emphasis&#125;</code> in templates.
72-
73-
### The &#123;ui&#125; CSS Class String
74-
75-
Spec attributes automatically generate CSS classes through the <code>&#123;ui&#125;</code> variable:
76-
77-
```sui
78-
<div class="ui button &#123;ui&#125;">
79-
<!-- &#123;ui&#125; becomes "primary large" -->
80-
</div>
81-
```
8267

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
8769

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.
8971

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:
9173

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+
```
10187

102-
getCurrentStatus() {
103-
return settings.status;
104-
},
105-
}),
88+
```html title='page.html'
89+
<ui-button icon="save" red>Save</ui-button>
90+
```
10691

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>
111100
```
112101

102+
113103
## Spec Parts
114104

115105
Every spec has these top-level fields:
@@ -175,7 +165,7 @@ types: [
175165

176166
### States
177167

178-
Define how you component varies over time
168+
Define how your component varies over time
179169

180170
```javascript
181171
states: [
@@ -189,7 +179,7 @@ states: [
189179

190180
### Variations
191181

192-
Define stackable visual modifications:
182+
Define stackable visual modifications
193183

194184
```javascript
195185
variations: [
@@ -212,7 +202,7 @@ variations: [
212202

213203
### Settings
214204

215-
Define settings that modify how your component functions:
205+
Define settings that modify how your component functions
216206

217207
```javascript
218208
settings: [
@@ -234,7 +224,7 @@ settings: [
234224

235225
### Events
236226

237-
Define events dispatched by your components:
227+
Define events dispatched by your components
238228

239229
```javascript
240230
events: [

0 commit comments

Comments
 (0)