Skip to content

Commit

Permalink
feat(story-decorator): add story decorator to replace knobs for theme…
Browse files Browse the repository at this point in the history
… application
  • Loading branch information
Westbrook committed Jan 6, 2021
1 parent 143f0c1 commit 7c0c6be
Show file tree
Hide file tree
Showing 14 changed files with 518 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ packages/*/*/*.d.ts
!packages/*/test/global.d.ts
*.tsbuildinfo

projects/**/*.js
projects/**/*.js.map
projects/**/*.d.ts
!projects/*/global.d.ts

spectrum-web-components.build.log
spectrum-web-components.build.error.log
TESTS.xml
Expand Down
82 changes: 2 additions & 80 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,13 @@ import {
withA11y,
withKnobs,
withWebComponentsKnobs,
html,
select,
boolean,
} from '@open-wc/demoing-storybook';
import '@spectrum-web-components/theme/sp-theme.js';
import '@spectrum-web-components/theme/src/themes.js';

window.__swc_hack_knobs__ = window.__swc_hack_knobs__ || {};
import { swcThemeDecoratorWithConfig } from '@spectrum-web-components/story-decorator/decorator.js';

addDecorator(withA11y);
addDecorator(withKnobs);
addDecorator(withWebComponentsKnobs);
const colorOptions = {
Lightest: 'lightest',
Light: 'light',
Dark: 'dark',
Darkest: 'darkest',
};
window.__swc_hack_knobs__.defaultColor = colorOptions.Light;
const scaleOptions = {
Medium: 'medium',
Large: 'large',
};
window.__swc_hack_knobs__.defaultScale = scaleOptions.Medium;
const directionOptions = {
LTR: 'ltr',
RTL: 'rtl',
};
window.__swc_hack_knobs__.defaultDirection = 'ltr';
window.__swc_hack_knobs__.defaultReduceMotion = false;
addDecorator((story) => {
const color = select(
'Color',
colorOptions,
window.__swc_hack_knobs__.defaultColor,
'Theme'
);
window.__swc_hack_knobs__.defaultColor = color;
const scale = select(
'Scale',
scaleOptions,
window.__swc_hack_knobs__.defaultScale,
'Theme'
);
window.__swc_hack_knobs__.defaultScale = scale;
const dir = select(
'Text direction',
directionOptions,
window.__swc_hack_knobs__.defaultDirection,
'Theme'
);
window.__swc_hack_knobs__.defaultDirection = dir;
const reduceMotion = boolean(
'Reduce Motion',
window.__swc_hack_knobs__.defaultReduceMotion,
'Theme'
);
window.__swc_hack_knobs__.defaultReduceMotion = reduceMotion;
return html`
<sp-theme id="root-theme" color=${color} scale=${scale} dir=${dir}>
${reduceMotion
? html`
<style>
sp-theme {
--spectrum-global-animation-duration-0: 0ms;
--spectrum-global-animation-duration-100: 0ms;
--spectrum-global-animation-duration-200: 0ms;
--spectrum-global-animation-duration-300: 0ms;
--spectrum-global-animation-duration-400: 0ms;
--spectrum-global-animation-duration-500: 0ms;
--spectrum-global-animation-duration-600: 0ms;
--spectrum-global-animation-duration-700: 0ms;
--spectrum-global-animation-duration-800: 0ms;
--spectrum-global-animation-duration-900: 0ms;
--spectrum-global-animation-duration-1000: 0ms;
--spectrum-global-animation-duration-2000: 0ms;
--spectrum-global-animation-duration-4000: 0ms;
}
</style>
`
: html``}
${story()}
</sp-theme>
`;
});
addDecorator(swcThemeDecoratorWithConfig({ bundled: false }));

addParameters({
a11y: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"icons": "node ./scripts/process-icons.js && prettier --write 'packages/**/*.svg.ts'",
"icons:ui": "lerna run --scope @spectrum-web-components/icons-ui build",
"icons:workflow": "lerna run --scope @spectrum-web-components/icons-workflow build",
"build": "gulp css && tsc --build packages/**/tsconfig.json",
"build": "gulp css && tsc --build packages/**/tsconfig.json && tsc --build projects/story-decorator/tsconfig.json",
"build:watch": "tsc --build packages/**/tsconfig.json -w",
"build:tests": "tsc --build test/tsconfig.json && tsc --build test/tsconfig-node.json",
"build:clear-cache": "rimraf packages/*/lib && rimraf packages/*/tsconfig.tsbuildinfo",
Expand Down
9 changes: 6 additions & 3 deletions packages/base/src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ rtlObserver.observe(document.documentElement, {
attributeFilter: ['dir'],
});

type ContentDirectionManager = { startManagingContentDirection?(): void };
type ContentDirectionManager = HTMLElement & {
startManagingContentDirection?(): void;
};

const canManageContentDirection = (el: ContentDirectionManager): boolean =>
typeof el.startManagingContentDirection === 'undefined';
typeof el.startManagingContentDirection !== 'undefined' ||
el.tagName === 'SP-THEME';

export function SpectrumMixin<T extends Constructor<UpdatingElement>>(
constructor: T
Expand Down Expand Up @@ -78,7 +81,7 @@ export function SpectrumMixin<T extends Constructor<UpdatingElement>>(
this.parentNode) as HTMLElement;
while (
dirParent !== document.documentElement &&
canManageContentDirection(
!canManageContentDirection(
dirParent as ContentDirectionManager
)
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/overlay/stories/overlay.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ const storyStyles = html`
body,
#root,
#root-inner,
#root-theme {
sp-story-decorator {
height: 100%;
margin: 0;
}
#root-theme {
sp-story-decorator::part(container) {
display: flex;
flex-direction: column;
width: 100%;
Expand Down
28 changes: 28 additions & 0 deletions projects/story-decorator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Description

### Usage

[![See it on NPM!](https://img.shields.io/npm/v/@spectrum-web-components/story-decorator?style=for-the-badge)](https://www.npmjs.com/package/@spectrum-web-components/story-decorator)
[![How big is this package in your project?](https://img.shields.io/bundlephobia/minzip/@spectrum-web-components/story-decorator?style=for-the-badge)](https://bundlephobia.com/result?p=@spectrum-web-components/story-decorator)

```
yarn add @spectrum-web-components/story-decorator
```

Import the side effectful registration of `<sp-story-decorator>` via:

```
import '@spectrum-web-components/story-decorator/sp-story-decorator.js';
```

When looking to leverage the `StoryDecorator` base class as a type and/or for extension purposes, do so via:

```
import { StoryDecorator } from '@spectrum-web-components/story-decorator';
```

## Example

```html
<sp-story-decorator></sp-story-decorator>
```
43 changes: 43 additions & 0 deletions projects/story-decorator/decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import { html, TemplateResult, render } from 'lit-html';
import './sp-story-decorator.js';

export const themeStyles = html`
<style>
#root {
padding: 0;
}
</style>
`;

export const swcThemeDecoratorWithConfig = (
{ bundled } = { bundled: true }
) => (story: () => TemplateResult) => {
if (!bundled) {
requestAnimationFrame(() => {
const decorator = document.querySelector(
'sp-story-decorator'
) as HTMLElement;
render(story(), decorator);
});
}
return html`
${themeStyles}
<sp-story-decorator>
${bundled ? story() : html``}
</sp-story-decorator>
`;
};

export const swcThemeDecorator = swcThemeDecoratorWithConfig();
60 changes: 60 additions & 0 deletions projects/story-decorator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@spectrum-web-components/story-decorator",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/adobe/spectrum-web-components.git",
"directory": "packages/story-decorator"
},
"bugs": {
"url": "https://github.com/adobe/spectrum-web-components/issues"
},
"homepage": "https://adobe.github.io/spectrum-web-components/projects/story-decorator",
"keywords": [
"spectrum css",
"web components",
"lit-element",
"lit-html"
],
"version": "0.0.1-beta.10",
"description": "",
"main": "./src/index.js",
"module": "./src/index.js",
"types": "./src/index.d.ts",
"type": "module",
"exports": {
"./src/": "./src/",
"./custom-elements.json": "./custom-elements.json",
"./package.json": "./package.json",
"./decorator": "./decorator.js",
"./decorator.js": "./decorator.js",
"./sp-story-decorator": "./sp-story-decorator.js",
"./sp-story-decorator.js": "./sp-story-decorator.js"
},
"files": [
"custom-elements.json",
"*.ts",
"*.js",
"*.js.map",
"/src/"
],
"sideEffects": [
"./sp-*.js",
"./sp-*.ts"
],
"scripts": {
"test": "echo \"Error: run tests from mono-repo root.\" && exit 1"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@spectrum-web-components/base": "^0.1.3",
"@spectrum-web-components/theme": "^0.6.3",
"@spectrum-web-components/dropdown": "^0.8.5",
"@spectrum-web-components/menu": "^0.4.4",
"@spectrum-web-components/switch": "^0.4.4",
"tslib": "^2.0.0"
}
}
21 changes: 21 additions & 0 deletions projects/story-decorator/sp-story-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import { StoryDecorator } from './src/StoryDecorator.js';

customElements.define('sp-story-decorator', StoryDecorator);

declare global {
interface HTMLElementTagNameMap {
'sp-story-decorator': StoryDecorator;
}
}
Loading

0 comments on commit 7c0c6be

Please # to comment.