Skip to content

Commit d704159

Browse files
Melissa Thompsoncastastrophe
authored andcommitted
feat(downstate): docs + implementation for example components (#2520)
* feat(downstate): docs + implementation for example components * docs: update mdx * docs: reorg, stories live within foundations * docs: decorator for down state dimension tokens * docs: fix mdx hierarchy console error * fix: small iconOnly button gets min perspective * docs: use markdown, update language * fix: disabled, readonly checkbox doesnt have down state * chore(button,checkbox): update package versions
1 parent 863ae1d commit d704159

File tree

23 files changed

+193
-12
lines changed

23 files changed

+193
-12
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const withDownStateDimensionCapture = (selector) => (Story, context) => {
2+
const captureDownStateDimensions = () => {
3+
const components = document.querySelectorAll(selector);
4+
components.forEach((component) => {
5+
const { width, height } = component.getBoundingClientRect();
6+
component.style.setProperty('--spectrum-downstate-width', `${width}px`);
7+
component.style.setProperty('--spectrum-downstate-height', `${height}px`);
8+
});
9+
};
10+
11+
document.addEventListener("DOMContentLoaded", () => {
12+
// Wait to make sure the story is fully rendered (otherwise width/height can be wrong)
13+
setTimeout(() => {
14+
captureDownStateDimensions();
15+
}, 100);
16+
});
17+
18+
return Story(context);
19+
};

.storybook/decorators/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
export { withArgEvents } from "./arg-events.js";
1515
export { withContextWrapper } from "./context.js";
16+
export { withDownStateDimensionCapture } from "./down-state.js";
1617
export { withIconSpriteSheet } from "./icon-sprites.js";
1718
export { withLanguageWrapper } from "./language.js";
1819
export { withReducedMotionWrapper } from "./reduce-motion.js";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Template } from "../../../components/button/stories/template.js";
2+
3+
export default {
4+
title: "Foundations/Down state",
5+
description:
6+
"Buttons allow users to perform an action or to navigate to another page. They have multiple styles for various needs, and are ideal for calling attention to where a user needs to do something in order to move forward in a flow.",
7+
component: "Button",
8+
args: {
9+
rootClass: "spectrum-Button",
10+
},
11+
parameters: {
12+
actions: {
13+
handles: ['click .spectrum-Button'],
14+
},
15+
status: {
16+
type: process.env.MIGRATED_PACKAGES.includes("button")
17+
? "migrated"
18+
: undefined,
19+
},
20+
},
21+
tags: ['foundation'],
22+
};
23+
24+
export const ButtonDownState = Template.bind({});
25+
ButtonDownState.args = {
26+
label: "Edit",
27+
variant: "accent",
28+
customStyles: {
29+
"--spectrum-downstate-width": "72px",
30+
"--spectrum-downstate-height": "32px"
31+
}
32+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Template } from "../../../components/checkbox/stories/template";
2+
3+
export default {
4+
title: "Foundations/Down state",
5+
description:
6+
"Checkboxes allow users to select multiple items from a list of individual items, or mark one individual item as selected.",
7+
component: "Checkbox",
8+
args: {
9+
rootClass: "spectrum-Checkbox",
10+
},
11+
parameters: {
12+
actions: {
13+
handles: ['click input[type="checkbox"]'],
14+
},
15+
status: {
16+
type: process.env.MIGRATED_PACKAGES.includes("checkbox")
17+
? "migrated"
18+
: undefined,
19+
},
20+
},
21+
tags: ['foundation'],
22+
};
23+
24+
export const CheckboxDownState = Template.bind({});
25+
CheckboxDownState.args = {
26+
label: "Checkbox",
27+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Meta, Story } from '@storybook/blocks';
2+
import * as Checkbox from './checkbox-down-state.stories.js';
3+
import * as Button from './button-down-state.stories.js';
4+
5+
<Meta title="Down state" />
6+
7+
# Down state
8+
9+
Down state is a Spectrum 2 feature that creates the illusion of components being pressed-in when active. This functionality is already included in Spectrum 2 components that require down state in this project. It is implemented with a CSS transform. The implementation depends on the size of the interactable element, as shown in the examples below.
10+
11+
## Examples
12+
13+
### Minimum perspective
14+
15+
For elements that have a width of 24px or less, the minimum perspective token is used to apply the down state. One example of a component that uses this token is the checkbox:
16+
17+
<Story of={Checkbox.CheckboxDownState} />
18+
19+
In this case, we use the minimum perspective token:
20+
21+
```
22+
transform:
23+
perspective(var(--spectrum-component-size-minimum-perspective-down))
24+
translateZ(var(--spectrum-component-size-difference-down));
25+
```
26+
27+
### Calculated perspective
28+
29+
For elements that have a width of greater than 24px, we need to use the component's width and height to apply the down state. One example of a component that uses this logic is the button:
30+
31+
<Story of={Button.ButtonDownState} />
32+
33+
In this case, we use a max formula to calculate the perspective based on component width and height (this helps us account for components that may be very wide):
34+
35+
```
36+
transform:
37+
perspective(max(
38+
var(--spectrum-downstate-height),
39+
var(--spectrum-downstate-width) * var(--spectrum-component-size-width-ratio-down)
40+
))
41+
translateZ(var(--spectrum-component-size-difference-down));
42+
```
43+
44+
*Note that in this case, users are required to develop an implementation to determine the width and height of the component. Assign these values to the `--spectrum-downstate-width` and `--spectrum-downstate-height` custom properties in a `style` attribute on the HTML element to expose them for use in the CSS.*

.storybook/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export default {
2020
files: "*.@(stories.js|mdx)",
2121
titlePrefix: "Guides",
2222
},
23+
{
24+
directory: "./foundations",
25+
files: "*/*.mdx",
26+
titlePrefix: "Foundations",
27+
},
2328
{
2429
directory: "./deprecated",
2530
files: "**/*.@(stories.js|mdx)",

.storybook/manager.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,8 @@ addons.setConfig({
6464
}),
6565
sidebar: {
6666
showRoots: false,
67+
filters: {
68+
patterns: (item) => !(item.id.includes('forced-colors') || item.tags.includes('foundation')),
69+
}
6770
},
6871
});

.storybook/preview.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export const parameters = {
3232
order: [
3333
"Guides",
3434
["Contributing", "*", "Adobe Code of Conduct", "Changelog"],
35+
"Foundations",
36+
["*"],
3537
"Components",
3638
["*", ["Docs", "Default", "*"]],
3739
"Deprecated",

components/alertbanner/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
}
5050
},
5151
"devDependencies": {
52-
"@spectrum-css/button": "14.0.1",
52+
"@spectrum-css/button": "14.0.0-next.2",
5353
"@spectrum-css/closebutton": "5.0.0-next.0",
5454
"@spectrum-css/divider": "5.0.1",
5555
"@spectrum-css/icon": "9.0.1",

components/button/dist/metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@
202202
"--spectrum-component-pill-edge-to-visual-only-200",
203203
"--spectrum-component-pill-edge-to-visual-only-300",
204204
"--spectrum-component-pill-edge-to-visual-only-75",
205+
"--spectrum-component-size-difference-down",
206+
"--spectrum-component-size-minimum-perspective-down",
207+
"--spectrum-component-size-width-ratio-down",
205208
"--spectrum-component-top-to-workflow-icon-100",
206209
"--spectrum-component-top-to-workflow-icon-200",
207210
"--spectrum-component-top-to-workflow-icon-300",
@@ -215,6 +218,9 @@
215218
"--spectrum-disabled-static-white-background-color",
216219
"--spectrum-disabled-static-white-border-color",
217220
"--spectrum-disabled-static-white-content-color",
221+
"--spectrum-downstate-height",
222+
"--spectrum-downstate-perspective",
223+
"--spectrum-downstate-width",
218224
"--spectrum-focus-indicator-color",
219225
"--spectrum-focus-indicator-gap",
220226
"--spectrum-focus-indicator-thickness",

0 commit comments

Comments
 (0)