Skip to content

Commit 97257f3

Browse files
TarunAdobecastastrophe
authored andcommitted
chore: update styling guide (#5437)
* chore: add styling guide * chore: remove spectrum-config and update other docs * fix: update yarn new-package to not depend on spectrum-css * chore: update styling guide * chore: remove formating from plopfile
1 parent ac7f3a0 commit 97257f3

9 files changed

+129
-573
lines changed

PULL_REQUESTS.md

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ Incomplete templates may delay the review process.
102102
- `breaking-change`: PR contains changes that break backward compatibility
103103
- `help-wanted`: Extra attention is needed on this PR
104104
- `on-hold`: PR needs more discussion.
105-
- `Spectrum CSS`: An issue or pull request specific to the CSS being used by components.
106105
- `Component: [Name]`: PR effects this component
107106
- `auto-update`: Keep the base of the PR up-to-date with main automatically if there are no conflicts.
108107

projects/documentation/content/guides/adding-component.md

+1-289
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ slug: developing-component
1010
This guide explains the techniques involved in the ongoing development a Spectrum control
1111
as a <sp-link href="https://github.com/adobe/spectrum-web-components">spectrum-web-components</sp-link>.
1212

13-
The components in spectrum-web-components are based on the CSS definitions in
14-
<sp-link href="https://github.com/adobe/spectrum-css">spectrum-css</sp-link>. Typically, component
15-
implementations contain very little code. The CSS from the `spectrum-css`
16-
project typically specifies most, if not all, of the presentation details.
17-
1813
## What is a web component?
1914

2015
According to <sp-link href="https://www.webcomponents.org/introduction">webcomponents.org</sp-link>,
@@ -29,7 +24,6 @@ web components are:
2924
In order to add a new component to this library, you will need to develop a
3025
working knowledge of the following technologies:
3126

32-
- <sp-link href="https://github.com/adobe/spectrum-css">Spectrum CSS</sp-link>: A CSS implementation of the Spectrum design language
3327
- <sp-link href="https://developers.google.com/web/fundamentals/web-components/customelements">Web Components</sp-link>: Standards based method for adding new HTML tags to a browser
3428
- <sp-link href="https://developers.google.com/web/fundamentals/web-components/shadowdom">Shadow DOM</sp-link>: The part of the Web Component spec that allows for encapsulation of component styles and child nodes
3529
- <sp-link href="https://lit-element.polymer-project.org/guide">lit-element</sp-link>: A simple base class for creating fast, lightweight web components
@@ -45,81 +39,7 @@ the heart of a web component. It isolates the component from the styles and DOM
4539
of the containing page. While this offers many benefits, it also means that we
4640
must structure our CSS very differently.
4741

48-
The CSS from the <sp-link href="https://github.com/adobe/spectrum-css">spectrum-css</sp-link> project
49-
is intended to be installed globally on a web page. Using it in the context of a
50-
web component requires that we modify it. To facilitate that, this project comes
51-
with a <sp-link href="https://github.com/adobe/spectrum-web-components/blob/master/tasks/process-spectrum.js">config-driven processor</sp-link> that can transform the Spectrum CSS into a format
52-
that can be consumed in a web component.
53-
54-
The first step is to create a directory and a `spectrum-config.js` file for your
55-
new component. This config file contains information about the structure of
56-
the web component in relation to the Spectrum CSS classes.
57-
58-
Below is a fragment of the <sp-link href="https://github.com/adobe/spectrum-web-components/blob/master/src/button/spectrum-config.js">`spectrum-config.js` file for `sp-button`</sp-link>.
59-
60-
```javascript
61-
const config = {
62-
conversions: [
63-
{
64-
inPackage: '@spectrum-css/button',
65-
outPackage: 'button',
66-
fileName: 'button',
67-
excludeByComponents: [builder.element('a')],
68-
components: [
69-
converter.classToHost(),
70-
converter.classToAttribute('spectrum-Button--quiet'),
71-
converter.classToAttribute('is-disabled', 'disabled'),
72-
converter.pseudoToAttribute('disabled', 'disabled'),
73-
...converter.enumerateAttributes(
74-
[
75-
['spectrum-Button--sizeS', 's'],
76-
['spectrum-Button--sizeM', 'm'],
77-
['spectrum-Button--sizeL', 'l'],
78-
['spectrum-Button--sizeXL', 'xl'],
79-
],
80-
'size'
81-
),
82-
converter.classToId('spectrum-Button-label'),
83-
converter.classToSlotted('spectrum-Icon', 'icon'),
84-
{
85-
find: [
86-
builder.class('spectrum-Icon'),
87-
builder.combinator('+'),
88-
builder.class('spectrum-Button-label'),
89-
],
90-
replace: [
91-
{
92-
replace: builder.attribute('name', 'icon', 'equal'),
93-
hoist: false,
94-
},
95-
builder.combinator('+'),
96-
builder.id('label'),
97-
],
98-
},
99-
{
100-
hoist: false,
101-
find: builder.pseudoClass('empty'),
102-
replace: builder.attribute('hidden'),
103-
},
104-
],
105-
},
106-
],
107-
};
108-
```
109-
110-
If we wanted to create a button component using this config file, the steps would be as
111-
follows:
112-
113-
1. Make the directory <sp-link href="https://github.com/adobe/spectrum-web-components/tree/master/src/button">`src/components/button`</sp-link>
114-
2. In that new directory, create a <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/button/src/spectrum-config.js">`spectrum-config.js`</sp-link>
115-
file with the above contents
116-
3. Run the command `yarn process-spectrum` to create the <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/button/src/spectrum-button.css">CSS file</sp-link>
117-
118-
When you do the above, the <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/scripts/process-spectrum-postcss-plugin.js">config-driven processor</sp-link>
119-
will look in the <sp-link href="https://github.com/adobe/spectrum-css">`spectrum-css`</sp-link> project
120-
for the <sp-link href="https://unpkg.com/@spectrum-css/button/dist/index-vars.css">matching CSS file</sp-link>.
121-
It will parse that file and restructure the CSS as per the configuration
122-
instructions.
42+
For more information on how to structure your CSS, see the [Styling](/guides/styling-components) guide.
12343

12444
## Structure of a Spectrum Web Component
12545

@@ -143,214 +63,6 @@ structure that looks like this.
14363
If anything here looks unfamiliar, it is probably a good time to do some reading
14464
about <sp-link href="https://developers.google.com/web/fundamentals/web-components/customelements">web components</sp-link>.
14565
146-
You can compare this markup with the <sp-link href="http://opensource.adobe.com/spectrum-css/2.13.0/docs/#button---cta">reference markup in the `spectrum-css` documentation</sp-link>
147-
148-
### Host Class Mapping
149-
150-
We need to determine what the main CSS class is for our component in the
151-
original `spectrum-css`. In the case of `sp-button`, we can see that the
152-
top-level class is `.Spectrum-Button`. We then need to determine where we want
153-
that CSS to be applied. In many cases, you will want that CSS to be applied to
154-
the actual web component via the `:host` selector. That is the default behaviour
155-
of the conversion script. In this case, we wanted to preserve all of the default
156-
behaviour of the `button` element in HTML. So, we want the main CSS to be
157-
applied to our `<button>` instead. If you look at the <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/button/src/spectrum-config.js">`host` definition in
158-
`spectrum-config.js`</sp-link>
159-
you can see that we have supplied a `shadowSelector` option. That tells the
160-
script to move all of the CSS for `.Spectrum-Button` to the `#button` element in
161-
the shadow DOM.
162-
163-
```javascript
164-
host: {
165-
selector: '.spectrum-Button',
166-
shadowSelector: '#button',
167-
},
168-
```
169-
170-
### Shadow DOM Structure
171-
172-
The next step is to fill out the remaining structure of the shadow DOM portion
173-
of the component. Note that, in the shadow DOM, we are using ids instead of long
174-
class names. We can do that because the namespace of each instance of our web
175-
component has it's own DOM scope. So, there can never be an id name collision.
176-
177-
Typically, you will reference the <sp-link href="http://opensource.adobe.com/spectrum-css/2.13.0/docs/#checkbox">sample code from the
178-
`spectrum-css`</sp-link>
179-
documentation and <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/checkbox/src/Checkbox.ts">recreate that structure in the shadow DOM of your
180-
component</sp-link>.
181-
182-
In the case of `sp-checkbox`, we turn this sample DOM code:
183-
184-
```html-no-demo
185-
<label class="spectrum-Checkbox">
186-
<input type="checkbox" class="spectrum-Checkbox-input" id="checkbox-0">
187-
<span class="spectrum-Checkbox-box">
188-
<svg class="spectrum-Icon spectrum-UIIcon-CheckmarkSmall spectrum-Checkbox-checkmark" focusable="false" aria-hidden="true">
189-
<use xlink:href="#spectrum-css-icon-CheckmarkSmall" />
190-
</svg>
191-
<svg class="spectrum-Icon spectrum-UIIcon-DashSmall spectrum-Checkbox-partialCheckmark" focusable="false" aria-hidden="true">
192-
<use xlink:href="#spectrum-css-icon-DashSmall" />
193-
</svg>
194-
</span>
195-
<span class="spectrum-Checkbox-label">Checkbox</span>
196-
</label>
197-
```
198-
199-
into this code in our component's render method (actually implementation is
200-
slightly different):
201-
202-
```javascript
203-
return html`
204-
<label id="root">
205-
<input
206-
id="input"
207-
type="checkbox"
208-
?checked=${this.checked}
209-
@change=${this.handleChange}
210-
<span id="box">
211-
<sp-icon
212-
id="checkmark"
213-
size="s"
214-
name="ui:CheckmarkSmall"
215-
aria-hidden="true"
216-
></sp-icon>
217-
<sp-icon
218-
id="partialCheckmark"
219-
size="s"
220-
name="ui:DashSmall"
221-
aria-hidden="true"
222-
></sp-icon>
223-
</span>
224-
<span id="label"><slot></slot></span>
225-
</label>
226-
`;
227-
```
228-
229-
You will notice that many of the `spectrum-css` classes are mapped to ids in the
230-
web component. For example, `.spectrum-Checkbox-input` and
231-
`.spectrum-Checkbox-box` become `#input` and `#box`. Those transformations are
232-
described in the <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/checkbox/src/spectrum-config.js">`ids` section of the `spectrum-config.js`
233-
file</sp-link>.
234-
235-
```javascript
236-
ids: [
237-
{
238-
selector: '.spectrum-Checkbox-input',
239-
name: 'input',
240-
},
241-
{
242-
selector: '.spectrum-Checkbox-box',
243-
name: 'box',
244-
},
245-
{
246-
selector: '.spectrum-Checkbox-checkmark',
247-
name: 'checkmark',
248-
},
249-
{
250-
selector: '.spectrum-Checkbox-partialCheckmark',
251-
name: 'partialCheckmark',
252-
},
253-
{
254-
selector: '.spectrum-Checkbox-label',
255-
name: 'label',
256-
},
257-
],
258-
```
259-
260-
### Properties and Attributes
261-
262-
Most of our controls have options that affect how they are rendered. For
263-
example, Spectrum supports a number of different kinds of buttons (e.g primary,
264-
secondary or call-to-action). `spectrum-css` supports these visual styles using
265-
CSS classes. In web components, we typically support these options using
266-
attributes/properties on the component. For example, here is a call-to-action
267-
style button.
268-
269-
```html
270-
<sp-button variant="accent">CTA</sp-button>
271-
```
272-
273-
We could conditionally add CSS classes to the elements of the shadow DOM during
274-
rendering, but it is much easier to just let the attributes on the DOM node
275-
drive the styling directly. In order to facilitate that, the
276-
<sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/button/src/spectrum-config.js">`spectrum-config.js` file lets you specify how to map the various
277-
`spectrum-css` classes to CSS that is based on the attributes on the `:host` of
278-
the web
279-
component</sp-link>.
280-
281-
```javascript
282-
attributes: [
283-
{
284-
type: 'boolean',
285-
selector: '.spectrum-Button--quiet',
286-
},
287-
{
288-
type: 'boolean',
289-
selector: ':disabled',
290-
},
291-
{
292-
type: 'enum',
293-
name: 'variant',
294-
values: [
295-
'.spectrum-Button--cta',
296-
'.spectrum-Button--primary',
297-
'.spectrum-Button--secondary',
298-
{
299-
name: 'negative',
300-
selector: '.spectrum-Button--warning',
301-
},
302-
'.spectrum-Button--overBackground',
303-
'.spectrum-Button--secondary',
304-
],
305-
},
306-
],
307-
```
308-
309-
We support two different kinds of attributes, booleans and enums. Booleans are
310-
turned on or off by the presence or absence of the attribute. Enum attributes
311-
must be set to one of the allowed values. The <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/button/src/spectrum-button.css">CSS generated will reference the
312-
attributes on the `host:` element
313-
directly</sp-link>.
314-
315-
### Class to Class Mapping
316-
317-
In some cases, you will need to retain the `spectrum-css` classes as classes. An
318-
example of that is when you need to apply CSS rules to multiple items in the
319-
shadow DOM. In that case, we simply map class names to shorter classnames. There
320-
is an <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/slider/src/spectrum-config.js">example of remapping classes in the slider
321-
component</sp-link>.
322-
323-
```javascript
324-
classes: [
325-
{
326-
selector: '.spectrum-Slider-track',
327-
name: 'track',
328-
},
329-
],
330-
```
331-
332-
### Slots
333-
334-
<sp-link href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot">Slot tags</sp-link> are
335-
how we host our child content (light DOM) in our component's shadow DOM. The
336-
`spectrum-css` for a component sometimes contains rules for laying out the child
337-
content. There is a <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/button/src/spectrum-config.js">`slots`
338-
section</sp-link>
339-
in the `spectrum-config.js` file for mapping those rules to the slotted content.
340-
341-
```javascript
342-
slots: [
343-
{
344-
name: 'icon',
345-
selector: '.spectrum-Icon',
346-
},
347-
],
348-
```
349-
350-
The above section tells our CSS processor to map CSS for the `.spectrum-Icon`
351-
class to the content that is being hosted in the <sp-link href="https://github.com/adobe/spectrum-web-components/blob/main/packages/button/src/spectrum-button.css">slot with the name
352-
`icon`</sp-link>.
353-
35466
## Coding the Component
35567
35668
All of the `spectrum-web-components` are written using the

0 commit comments

Comments
 (0)