Skip to content

Commit 153ce43

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 ddf5add commit 153ce43

File tree

14 files changed

+170
-3
lines changed

14 files changed

+170
-3
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
@@ -68,5 +68,8 @@ addons.setConfig({
6868
}),
6969
sidebar: {
7070
showRoots: false,
71+
filters: {
72+
patterns: (item) => !(item.id.includes('forced-colors') || item.tags.includes('foundation')),
73+
}
7174
},
7275
});

.storybook/preview.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export const parameters = {
3535
order: [
3636
"Guides",
3737
["Contributing", "*", "Adobe Code of Conduct", "Changelog"],
38+
"Foundations",
39+
["*"],
3840
"Components",
3941
["*", ["Docs", "Default", "*"]],
4042
"Deprecated",

components/button/dist/metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
"--spectrum-component-pill-edge-to-visual-only-200",
196196
"--spectrum-component-pill-edge-to-visual-only-300",
197197
"--spectrum-component-pill-edge-to-visual-only-75",
198+
"--spectrum-component-size-difference-down",
199+
"--spectrum-component-size-minimum-perspective-down",
200+
"--spectrum-component-size-width-ratio-down",
198201
"--spectrum-component-top-to-workflow-icon-100",
199202
"--spectrum-component-top-to-workflow-icon-200",
200203
"--spectrum-component-top-to-workflow-icon-300",
@@ -208,6 +211,9 @@
208211
"--spectrum-disabled-static-white-background-color",
209212
"--spectrum-disabled-static-white-border-color",
210213
"--spectrum-disabled-static-white-content-color",
214+
"--spectrum-downstate-height",
215+
"--spectrum-downstate-perspective",
216+
"--spectrum-downstate-width",
211217
"--spectrum-focus-indicator-color",
212218
"--spectrum-focus-indicator-gap",
213219
"--spectrum-focus-indicator-thickness",

components/button/index.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
--spectrum-button-content-color-focus: var(--highcontrast-button-content-color-focus, var(--mod-button-content-color-focus, var(--spectrum-neutral-content-color-key-focus)));
5757
--spectrum-button-content-color-disabled: var(--highcontrast-button-content-color-disabled, var(--mod-button-content-color-disabled, var(--spectrum-disabled-content-color)));
5858

59+
/* stylelint-disable-next-line spectrum-tools/no-unknown-custom-properties */
60+
--spectrum-downstate-perspective: max(var(--spectrum-downstate-height), var(--spectrum-downstate-width) * var(--spectrum-component-size-width-ratio-down));
61+
62+
&.spectrum-Button--iconOnly {
63+
--spectrum-downstate-perspective: var(--spectrum-component-size-minimum-perspective-down);
64+
}
5965
}
6066

6167
.spectrum-Button--sizeS {
@@ -436,6 +442,8 @@
436442
border-color: var(--highcontrast-button-border-color-down, var(--mod-button-border-color-down, var(--spectrum-button-border-color-down)));
437443
color: var(--spectrum-button-content-color-down);
438444
box-shadow: none;
445+
446+
transform: perspective(var(--spectrum-downstate-perspective)) translateZ(var(--spectrum-component-size-difference-down));
439447
}
440448

441449
&:disabled,

0 commit comments

Comments
 (0)