storybook-addon-pseudo-states 1.15.4 → 1.15.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ import { rewriteStyleSheet } from "./rewriteStyleSheet";
2
+ class Sheet {
3
+ constructor() {
4
+ this.__pseudoStatesRewritten = false;
5
+ for (var _len = arguments.length, rules = new Array(_len), _key = 0; _key < _len; _key++) {
6
+ rules[_key] = arguments[_key];
7
+ }
8
+ this.cssRules = rules.map(cssText => ({
9
+ cssText,
10
+ selectorText: cssText.slice(0, cssText.indexOf(" {"))
11
+ }));
12
+ }
13
+ deleteRule(index) {
14
+ this.cssRules.splice(index, 1);
15
+ }
16
+ insertRule(cssText, index) {
17
+ this.cssRules.splice(index, 0, cssText);
18
+ }
19
+ }
20
+ describe("rewriteStyleSheet", () => {
21
+ it("adds alternative selector targeting the element directly", () => {
22
+ const sheet = new Sheet("a:hover { color: red }");
23
+ rewriteStyleSheet(sheet);
24
+ expect(sheet.cssRules[0]).toContain("a.pseudo-hover");
25
+ });
26
+ it("adds alternative selector targeting an ancestor", () => {
27
+ const sheet = new Sheet("a:hover { color: red }");
28
+ rewriteStyleSheet(sheet);
29
+ expect(sheet.cssRules[0]).toContain(".pseudo-hover a");
30
+ });
31
+ it("does not add .pseudo-<class> to pseudo-class, which does not support classes", () => {
32
+ const sheet = new Sheet("::-webkit-scrollbar-thumb:hover { border-color: transparent; }");
33
+ rewriteStyleSheet(sheet);
34
+ console.log(sheet.cssRules[0]);
35
+ expect(sheet.cssRules[0]).not.toContain("::-webkit-scrollbar-thumb.pseudo-hover");
36
+ });
37
+ it("adds alternative selector for each pseudo selector", () => {
38
+ const sheet = new Sheet("a:hover, a:focus { color: red }");
39
+ rewriteStyleSheet(sheet);
40
+ expect(sheet.cssRules[0]).toContain("a.pseudo-hover");
41
+ expect(sheet.cssRules[0]).toContain("a.pseudo-focus");
42
+ expect(sheet.cssRules[0]).toContain(".pseudo-hover a");
43
+ expect(sheet.cssRules[0]).toContain(".pseudo-focus a");
44
+ });
45
+ it("keeps non-pseudo selectors as-is", () => {
46
+ const sheet = new Sheet("a.class, a:hover, a:focus, a#id { color: red }");
47
+ rewriteStyleSheet(sheet);
48
+ expect(sheet.cssRules[0]).toContain("a.class");
49
+ expect(sheet.cssRules[0]).toContain("a#id");
50
+ });
51
+ it("supports combined pseudo selectors", () => {
52
+ const sheet = new Sheet("a:hover:focus { color: red }");
53
+ rewriteStyleSheet(sheet);
54
+ expect(sheet.cssRules[0]).toContain("a.pseudo-hover.pseudo-focus");
55
+ expect(sheet.cssRules[0]).toContain(".pseudo-hover.pseudo-focus a");
56
+ });
57
+ it('supports ":host"', () => {
58
+ const sheet = new Sheet(":host(:hover) { color: red }");
59
+ rewriteStyleSheet(sheet);
60
+ expect(sheet.cssRules[0]).toEqual(":host(:hover), :host(.pseudo-hover) { color: red }");
61
+ });
62
+ it('supports ":not"', () => {
63
+ const sheet = new Sheet(":not(:hover) { color: red }");
64
+ rewriteStyleSheet(sheet);
65
+ expect(sheet.cssRules[0]).toEqual(":not(:hover), :not(.pseudo-hover) { color: red }");
66
+ });
67
+ });
@@ -0,0 +1,29 @@
1
+ const isAtRule = selector => selector.indexOf("@") === 0;
2
+ export const splitSelectors = selectors => {
3
+ if (isAtRule(selectors)) return [selectors];
4
+ let result = [];
5
+ let parentheses = 0;
6
+ let brackets = 0;
7
+ let selector = "";
8
+ for (let i = 0, len = selectors.length; i < len; i++) {
9
+ const char = selectors[i];
10
+ if (char === "(") {
11
+ parentheses += 1;
12
+ } else if (char === ")") {
13
+ parentheses -= 1;
14
+ } else if (char === "[") {
15
+ brackets += 1;
16
+ } else if (char === "]") {
17
+ brackets -= 1;
18
+ } else if (char === ",") {
19
+ if (!parentheses && !brackets) {
20
+ result.push(selector.trim());
21
+ selector = "";
22
+ continue;
23
+ }
24
+ }
25
+ selector += char;
26
+ }
27
+ result.push(selector.trim());
28
+ return result;
29
+ };
@@ -0,0 +1,15 @@
1
+ import { splitSelectors } from "./splitSelectors";
2
+ describe("splitSelectors", () => {
3
+ test("handles basic selectors", () => {
4
+ expect(splitSelectors(".a")).toEqual([".a"]);
5
+ expect(splitSelectors(".a, .b")).toEqual([".a", ".b"]);
6
+ });
7
+ test("supports ::slotted and :is", () => {
8
+ expect(splitSelectors("::slotted(:is(button, a):active)")).toEqual(["::slotted(:is(button, a):active)"]);
9
+ expect(splitSelectors("::slotted(:is(button, a):active), ::slotted(:is(button, a):hover)")).toEqual(["::slotted(:is(button, a):active)", "::slotted(:is(button, a):hover)"]);
10
+ });
11
+ test("supports :host", () => {
12
+ expect(splitSelectors(":host([type='secondary']) ::slotted(:is(button, a)), :host([type='primary']) ::slotted(:is(button, a):active)")).toEqual([":host([type='secondary']) ::slotted(:is(button, a))", ":host([type='primary']) ::slotted(:is(button, a):active)"]);
13
+ expect(splitSelectors(":host([outline]) ::slotted(:is(button, a):focus-within:focus-visible:not(:active))")).toEqual([":host([outline]) ::slotted(:is(button, a):focus-within:focus-visible:not(:active))"]);
14
+ });
15
+ });
@@ -0,0 +1,121 @@
1
+ /* eslint-env browser */
2
+ import { addons, useEffect } from "@storybook/addons";
3
+ import { DOCS_RENDERED, STORY_CHANGED, STORY_RENDERED, UPDATE_GLOBALS } from "@storybook/core-events";
4
+ import { PSEUDO_STATES } from "./constants";
5
+ import { rewriteStyleSheet } from "./rewriteStyleSheet";
6
+ const channel = addons.getChannel();
7
+ const shadowHosts = new Set();
8
+
9
+ // Drops any existing pseudo state classnames that carried over from a previously viewed story
10
+ // before adding the new classnames. We do this the old-fashioned way, for IE compatibility.
11
+ const applyClasses = (element, classnames) => {
12
+ element.className = element.className.split(" ").filter(classname => classname && classname.indexOf("pseudo-") !== 0).concat(...classnames).join(" ");
13
+ };
14
+ const applyParameter = (rootElement, parameter) => {
15
+ const map = new Map([[rootElement, new Set()]]);
16
+ const add = (target, state) => map.set(target, new Set([...(map.get(target) || []), state]));
17
+ Object.entries(parameter || {}).forEach(_ref => {
18
+ let [state, value] = _ref;
19
+ if (typeof value === "boolean") {
20
+ // default API - applying pseudo class to root element.
21
+ add(rootElement, value && state);
22
+ } else if (typeof value === "string") {
23
+ // explicit selectors API - applying pseudo class to a specific element
24
+ rootElement.querySelectorAll(value).forEach(el => add(el, state));
25
+ } else if (Array.isArray(value)) {
26
+ // explicit selectors API - we have an array (of strings) recursively handle each one
27
+ value.forEach(sel => rootElement.querySelectorAll(sel).forEach(el => add(el, state)));
28
+ }
29
+ });
30
+ map.forEach((states, target) => {
31
+ const classnames = [];
32
+ states.forEach(key => PSEUDO_STATES[key] && classnames.push(`pseudo-${PSEUDO_STATES[key]}`));
33
+ applyClasses(target, classnames);
34
+ });
35
+ };
36
+
37
+ // Traverses ancestry to collect relevant pseudo classnames, and applies them to the shadow host.
38
+ // Shadow DOM can only access classes on its host. Traversing is needed to mimic the CSS cascade.
39
+ const updateShadowHost = shadowHost => {
40
+ const classnames = new Set();
41
+ for (let element = shadowHost.parentElement; element; element = element.parentElement) {
42
+ if (!element.className) continue;
43
+ element.className.split(" ").filter(classname => classname.indexOf("pseudo-") === 0).forEach(classname => classnames.add(classname));
44
+ }
45
+ applyClasses(shadowHost, classnames);
46
+ };
47
+
48
+ // Global decorator that rewrites stylesheets and applies classnames to render pseudo styles
49
+ export const withPseudoState = (StoryFn, _ref2) => {
50
+ let {
51
+ viewMode,
52
+ parameters,
53
+ id,
54
+ globals: globalsArgs
55
+ } = _ref2;
56
+ const {
57
+ pseudo: parameter
58
+ } = parameters;
59
+ const {
60
+ pseudo: globals
61
+ } = globalsArgs;
62
+
63
+ // Sync parameter to globals, used by the toolbar (only in canvas as this
64
+ // doesn't make sense for docs because many stories are displayed at once)
65
+ useEffect(() => {
66
+ if (parameter !== globals && viewMode === "story") {
67
+ channel.emit(UPDATE_GLOBALS, {
68
+ globals: {
69
+ pseudo: parameter
70
+ }
71
+ });
72
+ }
73
+ }, [parameter, viewMode]);
74
+
75
+ // Convert selected states to classnames and apply them to the story root element.
76
+ // Then update each shadow host to redetermine its own pseudo classnames.
77
+ useEffect(() => {
78
+ const timeout = setTimeout(() => {
79
+ const element = document.getElementById(viewMode === "docs" ? `story--${id}` : `root`);
80
+ applyParameter(element, globals || parameter);
81
+ shadowHosts.forEach(updateShadowHost);
82
+ }, 0);
83
+ return () => clearTimeout(timeout);
84
+ }, [globals, parameter, viewMode]);
85
+ return StoryFn();
86
+ };
87
+
88
+ // Rewrite CSS rules for pseudo-states on all stylesheets to add an alternative selector
89
+ const rewriteStyleSheets = shadowRoot => {
90
+ let styleSheets = shadowRoot ? shadowRoot.styleSheets : document.styleSheets;
91
+ if (shadowRoot && shadowRoot.adoptedStyleSheets ? shadowRoot.adoptedStyleSheets.length : undefined) styleSheets = shadowRoot.adoptedStyleSheets;
92
+ Array.from(styleSheets).forEach(sheet => rewriteStyleSheet(sheet, shadowRoot, shadowHosts));
93
+ };
94
+
95
+ // Only track shadow hosts for the current story
96
+ channel.on(STORY_CHANGED, () => shadowHosts.clear());
97
+
98
+ // Reinitialize CSS enhancements every time the story changes
99
+ channel.on(STORY_RENDERED, () => rewriteStyleSheets());
100
+
101
+ // Reinitialize CSS enhancements every time a docs page is rendered
102
+ channel.on(DOCS_RENDERED, () => rewriteStyleSheets());
103
+
104
+ // IE doesn't support shadow DOM
105
+ if (Element.prototype.attachShadow) {
106
+ // Monkeypatch the attachShadow method so we can handle pseudo styles inside shadow DOM
107
+ Element.prototype._attachShadow = Element.prototype.attachShadow;
108
+ Element.prototype.attachShadow = function attachShadow(init) {
109
+ // Force "open" mode, so we can access the shadowRoot
110
+ const shadowRoot = this._attachShadow({
111
+ ...init,
112
+ mode: "open"
113
+ });
114
+ // Wait for it to render and apply its styles before rewriting them
115
+ requestAnimationFrame(() => {
116
+ rewriteStyleSheets(shadowRoot);
117
+ updateShadowHost(shadowRoot.host);
118
+ });
119
+ return shadowRoot;
120
+ };
121
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-addon-pseudo-states",
3
- "version": "1.15.4",
3
+ "version": "1.15.5",
4
4
  "description": "CSS pseudo states for Storybook",
5
5
  "keywords": [
6
6
  "storybook-addons",
@@ -14,71 +14,49 @@
14
14
  },
15
15
  "author": "ghengeveld",
16
16
  "license": "MIT",
17
- "main": "dist/index.js",
18
- "module": "dist/index.mjs",
19
- "types": "dist/index.d.ts",
20
- "exports": {
21
- ".": {
22
- "require": "./dist/index.js",
23
- "import": "./dist/index.mjs",
24
- "types": "./dist/index.d.ts"
25
- },
26
- "./manager": {
27
- "require": "./dist/manager.js",
28
- "import": "./dist/manager.mjs",
29
- "types": "./dist/manager.d.ts"
30
- },
31
- "./preview": {
32
- "require": "./dist/preview.js",
33
- "import": "./dist/preview.mjs",
34
- "types": "./dist/preview.d.ts"
35
- },
36
- "./package.json": "./package.json"
37
- },
17
+ "main": "dist/cjs/preset.js",
18
+ "module": "dist/esm/preset.js",
38
19
  "files": [
39
- "*.js",
40
- "dist/**/*"
20
+ "dist/**/*",
21
+ "README.md",
22
+ "*.js"
41
23
  ],
42
24
  "scripts": {
43
25
  "clean": "rimraf ./dist",
44
26
  "start": "concurrently \"yarn storybook --no-manager-cache --quiet\" \"yarn build:dist --watch\"",
45
- "storybook": "storybook dev -p 6006",
27
+ "storybook": "start-storybook -p 6006",
46
28
  "test": "jest src",
47
- "chromatic": "npx chromatic",
48
- "build:dist": "tsup",
49
- "build:storybook": "storybook build",
29
+ "chromatic": "chromatic",
30
+ "build:dist": "babel ./src --out-dir ./dist/cjs && BABEL_ESM=\"true\" babel ./src --out-dir ./dist/esm",
31
+ "build:storybook": "build-storybook",
50
32
  "prepublish": "yarn clean && yarn build:dist",
51
33
  "release": "auto shipit --base-branch=main"
52
34
  },
53
35
  "dependencies": {},
54
36
  "devDependencies": {
37
+ "@babel/cli": "^7.19.3",
38
+ "@babel/core": "^7.20.5",
55
39
  "@babel/preset-env": "^7.20.2",
56
40
  "@babel/preset-react": "^7.18.6",
57
- "@babel/preset-typescript": "^7.18.6",
58
- "@storybook/addon-essentials": "^7.0.0-beta.3",
59
- "@storybook/addon-interactions": "^7.0.0-beta.3",
60
- "@storybook/addon-links": "^7.0.0-beta.3",
61
- "@storybook/react": "^7.0.0-beta.3",
62
- "@storybook/react-webpack5": "^7.0.0-beta.3",
63
- "@storybook/testing-library": "^0.0.13",
64
- "@storybook/types": "^7.0.0-beta.3",
65
- "@types/jest": "^29.2.4",
41
+ "@storybook/addon-docs": "^6.5.14",
42
+ "@storybook/builder-webpack5": "^6.5.14",
43
+ "@storybook/manager-webpack5": "^6.5.14",
44
+ "@storybook/react": "^6.5.14",
66
45
  "auto": "^10.16.8",
46
+ "babel-loader": "^9.1.0",
47
+ "chromatic": "^6.12.0",
67
48
  "concurrently": "^5.3.0",
68
49
  "jest": "^27.5.1",
69
50
  "react": "^17.0.1",
70
51
  "react-dom": "^17.0.1",
71
- "rimraf": "^3.0.2",
72
- "storybook": "^7.0.0-beta.3",
73
- "tsup": "^6.5.0",
74
- "typescript": "^4.9.4"
52
+ "rimraf": "^3.0.2"
75
53
  },
76
54
  "peerDependencies": {
77
- "@storybook/components": "next",
78
- "@storybook/core-events": "next",
79
- "@storybook/manager-api": "next",
80
- "@storybook/preview-api": "next",
81
- "@storybook/theming": "next",
55
+ "@storybook/addons": "^6.5.0",
56
+ "@storybook/api": "^6.5.0",
57
+ "@storybook/components": "^6.5.0",
58
+ "@storybook/core-events": "^6.5.0",
59
+ "@storybook/theming": "^6.5.0",
82
60
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
83
61
  "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
84
62
  },
package/preset.js ADDED
@@ -0,0 +1,9 @@
1
+ function managerEntries(entry = []) {
2
+ return [...entry, require.resolve("./dist/esm/preset/manager")]
3
+ }
4
+
5
+ function config(entry = []) {
6
+ return [...entry, require.resolve("./dist/esm/preset/preview")]
7
+ }
8
+
9
+ module.exports = { managerEntries, config };
@@ -1,21 +0,0 @@
1
- // src/constants.ts
2
- var ADDON_ID = "storybook/pseudo-states";
3
- var TOOL_ID = `${ADDON_ID}/tool`;
4
- var EXCLUDED_PSEUDO_ELEMENTS = ["::-webkit-scrollbar-thumb"];
5
- var PSEUDO_STATES = {
6
- hover: "hover",
7
- active: "active",
8
- focusVisible: "focus-visible",
9
- focusWithin: "focus-within",
10
- focus: "focus",
11
- visited: "visited",
12
- link: "link",
13
- target: "target"
14
- };
15
-
16
- export {
17
- ADDON_ID,
18
- TOOL_ID,
19
- EXCLUDED_PSEUDO_ELEMENTS,
20
- PSEUDO_STATES
21
- };
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- declare const _default: {};
2
-
3
- export { _default as default };
package/dist/index.js DELETED
@@ -1,31 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- default: () => src_default
24
- });
25
- module.exports = __toCommonJS(src_exports);
26
- if (module && module.hot && module.hot.decline) {
27
- module.hot.decline();
28
- }
29
- var src_default = {};
30
- // Annotate the CommonJS export names for ESM import in node:
31
- 0 && (module.exports = {});
package/dist/index.mjs DELETED
@@ -1,8 +0,0 @@
1
- // src/index.ts
2
- if (module && module.hot && module.hot.decline) {
3
- module.hot.decline();
4
- }
5
- var src_default = {};
6
- export {
7
- src_default as default
8
- };
package/dist/manager.d.ts DELETED
@@ -1 +0,0 @@
1
-
package/dist/manager.js DELETED
@@ -1,96 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
18
- mod
19
- ));
20
-
21
- // src/manager.ts
22
- var import_manager_api2 = require("@storybook/manager-api");
23
-
24
- // src/constants.ts
25
- var ADDON_ID = "storybook/pseudo-states";
26
- var TOOL_ID = `${ADDON_ID}/tool`;
27
- var PSEUDO_STATES = {
28
- hover: "hover",
29
- active: "active",
30
- focusVisible: "focus-visible",
31
- focusWithin: "focus-within",
32
- focus: "focus",
33
- visited: "visited",
34
- link: "link",
35
- target: "target"
36
- };
37
-
38
- // src/manager/PseudoStateTool.tsx
39
- var import_react = __toESM(require("react"));
40
- var import_components = require("@storybook/components");
41
- var import_manager_api = require("@storybook/manager-api");
42
- var import_theming = require("@storybook/theming");
43
- var LinkTitle = import_theming.styled.span(({ active }) => ({
44
- color: active ? import_theming.color.secondary : "inherit"
45
- }));
46
- var LinkIcon = (0, import_theming.styled)(import_components.Icons)(({ active }) => ({
47
- opacity: active ? 1 : 0,
48
- path: { fill: active ? import_theming.color.secondary : "inherit" }
49
- }));
50
- var options = Object.keys(PSEUDO_STATES).sort();
51
- var PseudoStateTool = () => {
52
- const [{ pseudo }, updateGlobals] = (0, import_manager_api.useGlobals)();
53
- const isActive = (0, import_react.useCallback)((option) => pseudo?.[option] === true, [pseudo]);
54
- const toggleOption = (0, import_react.useCallback)(
55
- (option) => () => updateGlobals({ pseudo: { ...pseudo, [option]: !isActive(option) } }),
56
- [pseudo]
57
- );
58
- return /* @__PURE__ */ import_react.default.createElement(
59
- import_components.WithTooltip,
60
- {
61
- placement: "top",
62
- trigger: "click",
63
- tooltip: () => /* @__PURE__ */ import_react.default.createElement(
64
- import_components.TooltipLinkList,
65
- {
66
- links: options.map((option) => ({
67
- id: option,
68
- title: /* @__PURE__ */ import_react.default.createElement(LinkTitle, { active: isActive(option) }, ":", PSEUDO_STATES[option]),
69
- right: /* @__PURE__ */ import_react.default.createElement(LinkIcon, { icon: "check", width: 12, height: 12, active: isActive(option) }),
70
- onClick: toggleOption(option),
71
- active: isActive(option)
72
- }))
73
- }
74
- )
75
- },
76
- /* @__PURE__ */ import_react.default.createElement(
77
- import_components.IconButton,
78
- {
79
- key: "pseudo-state",
80
- title: "Select CSS pseudo states",
81
- active: options.some(isActive)
82
- },
83
- /* @__PURE__ */ import_react.default.createElement(import_components.Icons, { icon: "button" })
84
- )
85
- );
86
- };
87
-
88
- // src/manager.ts
89
- import_manager_api2.addons.register(ADDON_ID, () => {
90
- import_manager_api2.addons.add(TOOL_ID, {
91
- type: import_manager_api2.types.TOOL,
92
- title: "CSS pseudo states",
93
- match: ({ viewMode }) => viewMode === "story",
94
- render: PseudoStateTool
95
- });
96
- });
package/dist/manager.mjs DELETED
@@ -1,68 +0,0 @@
1
- import {
2
- ADDON_ID,
3
- PSEUDO_STATES,
4
- TOOL_ID
5
- } from "./chunk-A7FPDXSC.mjs";
6
-
7
- // src/manager.ts
8
- import { addons, types } from "@storybook/manager-api";
9
-
10
- // src/manager/PseudoStateTool.tsx
11
- import React, { useCallback } from "react";
12
- import { Icons, IconButton, WithTooltip, TooltipLinkList } from "@storybook/components";
13
- import { useGlobals } from "@storybook/manager-api";
14
- import { styled, color } from "@storybook/theming";
15
- var LinkTitle = styled.span(({ active }) => ({
16
- color: active ? color.secondary : "inherit"
17
- }));
18
- var LinkIcon = styled(Icons)(({ active }) => ({
19
- opacity: active ? 1 : 0,
20
- path: { fill: active ? color.secondary : "inherit" }
21
- }));
22
- var options = Object.keys(PSEUDO_STATES).sort();
23
- var PseudoStateTool = () => {
24
- const [{ pseudo }, updateGlobals] = useGlobals();
25
- const isActive = useCallback((option) => pseudo?.[option] === true, [pseudo]);
26
- const toggleOption = useCallback(
27
- (option) => () => updateGlobals({ pseudo: { ...pseudo, [option]: !isActive(option) } }),
28
- [pseudo]
29
- );
30
- return /* @__PURE__ */ React.createElement(
31
- WithTooltip,
32
- {
33
- placement: "top",
34
- trigger: "click",
35
- tooltip: () => /* @__PURE__ */ React.createElement(
36
- TooltipLinkList,
37
- {
38
- links: options.map((option) => ({
39
- id: option,
40
- title: /* @__PURE__ */ React.createElement(LinkTitle, { active: isActive(option) }, ":", PSEUDO_STATES[option]),
41
- right: /* @__PURE__ */ React.createElement(LinkIcon, { icon: "check", width: 12, height: 12, active: isActive(option) }),
42
- onClick: toggleOption(option),
43
- active: isActive(option)
44
- }))
45
- }
46
- )
47
- },
48
- /* @__PURE__ */ React.createElement(
49
- IconButton,
50
- {
51
- key: "pseudo-state",
52
- title: "Select CSS pseudo states",
53
- active: options.some(isActive)
54
- },
55
- /* @__PURE__ */ React.createElement(Icons, { icon: "button" })
56
- )
57
- );
58
- };
59
-
60
- // src/manager.ts
61
- addons.register(ADDON_ID, () => {
62
- addons.add(TOOL_ID, {
63
- type: types.TOOL,
64
- title: "CSS pseudo states",
65
- match: ({ viewMode }) => viewMode === "story",
66
- render: PseudoStateTool
67
- });
68
- });
package/dist/preview.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import * as _storybook_types from '@storybook/types';
2
-
3
- declare const decorators: _storybook_types.DecoratorFunction<_storybook_types.Renderer, _storybook_types.Args>[];
4
-
5
- export { decorators };