Skip to content

Commit 453cc1a

Browse files
Melissa Thompsonpfulton
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 f163dcd commit 453cc1a

File tree

13 files changed

+160
-8
lines changed

13 files changed

+160
-8
lines changed

.storybook/decorators/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { makeDecorator, useEffect } from "@storybook/preview-api";
22
import { html } from "lit";
33

44
export { withContextWrapper } from "./contextsWrapper.js";
5+
export { withDownStateDimensionCapture } from "./withDownStateDimensionCapture.js";
56
export { withTestingPreviewWrapper } from "./withTestingPreviewWrapper.js";
67

78
/**
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+
};
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";
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="Foundations/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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = {
1212
stories: [
1313
"../components/*/stories/*.stories.js",
1414
"./guides/*.mdx",
15+
"./foundations/*/*.mdx",
16+
"./foundations/*/*.stories.js",
1517
"./deprecated/*/*.stories.js",
1618
],
1719
rootDir: "../",

.storybook/manager.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@ addons.setConfig({
4949
}),
5050
sidebar: {
5151
showRoots: false,
52+
filters: {
53+
patterns: (item) => {
54+
return !item.tags.includes('foundation');
55+
}
56+
}
5257
},
5358
});

.storybook/preview.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import DocumentationTemplate from './DocumentationTemplate.mdx';
33

44
import { withActions } from "@storybook/addon-actions/decorator";
55
import {
6-
withContextWrapper,
7-
withLanguageWrapper,
8-
withReducedMotionWrapper,
9-
withTestingPreviewWrapper,
10-
withTextDirectionWrapper,
6+
withContextWrapper,
7+
withLanguageWrapper,
8+
withReducedMotionWrapper,
9+
withTestingPreviewWrapper,
10+
withTextDirectionWrapper,
1111
} from "./decorators/index.js";
1212

1313
// https://github.com/storybookjs/storybook-addon-console
@@ -190,7 +190,7 @@ export const parameters = {
190190
options: {
191191
storySort: {
192192
method: "alphabetical",
193-
order: ['Guides', ['Contributing', '*', 'Adobe Code of Conduct', 'Changelog'], 'Components', ['Docs', 'Default', '*'], '*'],
193+
order: ['Guides', ['Contributing', '*', 'Adobe Code of Conduct', 'Changelog'], 'Foundations', 'Components', ['Docs', 'Default', '*'], '*'],
194194
includeNames: true,
195195
},
196196
},

components/button/index.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ governing permissions and limitations under the License.
2626
--spectrum-button-focus-indicator-color: var(--spectrum-focus-indicator-color);
2727
--spectrum-button-intended-icon-size: var(--spectrum-workflow-icon-size-50);
2828

29+
<<<<<<< HEAD
2930
--mod-progress-circle-position: absolute;
31+
=======
32+
/* stylelint-disable-next-line spectrum-tools/no-unknown-custom-properties */
33+
--spectrum-downstate-perspective: max(var(--spectrum-downstate-height), var(--spectrum-downstate-width) * var(--spectrum-component-size-width-ratio-down));
34+
>>>>>>> 5a532e656 (feat(downstate): docs + implementation for example components (#2520))
3035
}
3136

3237
.spectrum-Button--sizeS {
@@ -45,6 +50,10 @@ governing permissions and limitations under the License.
4550
--spectrum-button-bottom-to-text: var(--spectrum-button-bottom-to-text-small);
4651
--spectrum-button-top-to-icon: var(--spectrum-component-top-to-workflow-icon-75);
4752
--spectrum-button-intended-icon-size: var(--spectrum-workflow-icon-size-75);
53+
54+
&.spectrum-Button--iconOnly {
55+
--spectrum-downstate-perspective: var(--spectrum-component-size-minimum-perspective-down);
56+
}
4857
}
4958

5059
.spectrum-Button--sizeM {
@@ -130,6 +139,10 @@ governing permissions and limitations under the License.
130139
box-shadow: none;
131140
}
132141

142+
&:active {
143+
transform: perspective(var(--spectrum-downstate-perspective)) translateZ(var(--spectrum-component-size-difference-down));
144+
}
145+
133146
.spectrum-Icon {
134147
/* Any block-size difference between the intended workflow icon size and actual icon used.
135148
Helps support any existing use of smaller UI icons instead of intended Workflow icons. */

components/button/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@spectrum-css/button",
3-
"version": "12.0.3-next.0",
3+
"version": "14.0.0-next.2",
44
"description": "The Spectrum CSS button component",
55
"license": "Apache-2.0",
66
"author": "Adobe",

0 commit comments

Comments
 (0)