storybook-addon-pseudo-states 1.0.2 → 1.0.3--canary.d752c59.0
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.
- package/CHANGELOG.md +0 -19
- package/dist/cjs/withPseudoState.js +37 -19
- package/dist/esm/withPseudoState.js +38 -21
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
# v1.0.0 (Fri Mar 11 2022)
|
|
2
|
-
|
|
3
|
-
#### 🐛 Bug Fix
|
|
4
|
-
|
|
5
|
-
- Add support for custom elements using `:host(:hover)` like styling. [#17](https://github.com/chromaui/storybook-addon-pseudo-states/pull/17) (jeroen.zwartepoorte@iddinkgroup.com [@ghengeveld](https://github.com/ghengeveld))
|
|
6
|
-
- Setup auto [#7](https://github.com/chromaui/storybook-addon-pseudo-states/pull/7) ([@ghengeveld](https://github.com/ghengeveld))
|
|
7
|
-
|
|
8
|
-
#### ⚠️ Pushed to `main`
|
|
9
|
-
|
|
10
|
-
- Upgrade Storybook ([@ghengeveld](https://github.com/ghengeveld))
|
|
11
|
-
- Ignore .env file ([@ghengeveld](https://github.com/ghengeveld))
|
|
12
|
-
|
|
13
|
-
#### Authors: 2
|
|
14
|
-
|
|
15
|
-
- Gert Hengeveld ([@ghengeveld](https://github.com/ghengeveld))
|
|
16
|
-
- Jeroen Zwartepoorte ([@jpzwarte](https://github.com/jpzwarte))
|
|
17
|
-
|
|
18
|
-
---
|
|
19
|
-
|
|
20
1
|
# v1.0.0 (Sat Feb 27 2021)
|
|
21
2
|
|
|
22
3
|
#### 🐛 Bug Fix
|
|
@@ -50,6 +50,21 @@ var applyClasses = function applyClasses(element, classnames) {
|
|
|
50
50
|
element.className = (_element$className$sp = element.className.split(" ").filter(function (classname) {
|
|
51
51
|
return classname && classname.indexOf("pseudo-") !== 0;
|
|
52
52
|
})).concat.apply(_element$className$sp, _toConsumableArray(classnames)).join(" ");
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
var applyParameter = function applyParameter(element, parameter) {
|
|
56
|
+
return applyClasses(element, Object.entries(parameter || {}).filter(function (_ref) {
|
|
57
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
58
|
+
_ = _ref2[0],
|
|
59
|
+
value = _ref2[1];
|
|
60
|
+
|
|
61
|
+
return value;
|
|
62
|
+
}).map(function (_ref3) {
|
|
63
|
+
var _ref4 = _slicedToArray(_ref3, 1),
|
|
64
|
+
key = _ref4[0];
|
|
65
|
+
|
|
66
|
+
return "pseudo-".concat(_constants.PSEUDO_STATES[key]);
|
|
67
|
+
}));
|
|
53
68
|
}; // Traverses ancestry to collect relevant pseudo classnames, and applies them to the shadow host.
|
|
54
69
|
// Shadow DOM can only access classes on its host. Traversing is needed to mimic the CSS cascade.
|
|
55
70
|
|
|
@@ -77,37 +92,35 @@ _addons.addons.getChannel().on(_coreEvents.STORY_CHANGED, function () {
|
|
|
77
92
|
}); // Global decorator that rewrites stylesheets and applies classnames to render pseudo styles
|
|
78
93
|
|
|
79
94
|
|
|
80
|
-
var withPseudoState = function withPseudoState(StoryFn) {
|
|
81
|
-
var
|
|
95
|
+
var withPseudoState = function withPseudoState(StoryFn, _ref5) {
|
|
96
|
+
var viewMode = _ref5.viewMode,
|
|
97
|
+
parameters = _ref5.parameters,
|
|
98
|
+
id = _ref5.id;
|
|
99
|
+
var parameter = parameters.pseudo;
|
|
82
100
|
|
|
83
101
|
var _useGlobals = (0, _addons.useGlobals)(),
|
|
84
102
|
_useGlobals2 = _slicedToArray(_useGlobals, 2),
|
|
85
103
|
globals = _useGlobals2[0].pseudo,
|
|
86
|
-
updateGlobals = _useGlobals2[1]; // Sync parameter to globals, used by the toolbar
|
|
104
|
+
updateGlobals = _useGlobals2[1]; // Sync parameter to globals, used by the toolbar (only in canvas as this
|
|
105
|
+
// doesn't make sense for docs because many stories are displayed at once)
|
|
87
106
|
|
|
88
107
|
|
|
89
108
|
(0, _addons.useEffect)(function () {
|
|
90
|
-
if (parameter !== globals) updateGlobals({
|
|
109
|
+
if (parameter !== globals && viewMode === "story") updateGlobals({
|
|
91
110
|
pseudo: parameter
|
|
92
111
|
});
|
|
93
|
-
}, [parameter]); // Convert selected states to classnames and apply them to the story root element.
|
|
112
|
+
}, [parameter, viewMode]); // Convert selected states to classnames and apply them to the story root element.
|
|
94
113
|
// Then update each shadow host to redetermine its own pseudo classnames.
|
|
95
114
|
|
|
96
115
|
(0, _addons.useEffect)(function () {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
key = _ref4[0];
|
|
106
|
-
|
|
107
|
-
return "pseudo-".concat(_constants.PSEUDO_STATES[key]);
|
|
108
|
-
}));
|
|
109
|
-
shadowHosts.forEach(updateShadowHost);
|
|
110
|
-
}, [globals]);
|
|
116
|
+
var timeout = setTimeout(function () {
|
|
117
|
+
applyParameter(document.getElementById(viewMode === "docs" ? "story--".concat(id) : "root"));
|
|
118
|
+
shadowHosts.forEach(updateShadowHost);
|
|
119
|
+
}, 0);
|
|
120
|
+
return function () {
|
|
121
|
+
return clearTimeout(timeout);
|
|
122
|
+
};
|
|
123
|
+
}, [parameter, viewMode]);
|
|
111
124
|
return StoryFn();
|
|
112
125
|
};
|
|
113
126
|
|
|
@@ -215,6 +228,11 @@ function rewriteStyleSheets(shadowRoot) {
|
|
|
215
228
|
|
|
216
229
|
_addons.addons.getChannel().on(_coreEvents.STORY_RENDERED, function () {
|
|
217
230
|
return rewriteStyleSheets();
|
|
231
|
+
}); // Reinitialize CSS enhancements every time a docs page is rendered
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
_addons.addons.getChannel().on(_coreEvents.DOCS_RENDERED, function () {
|
|
235
|
+
return rewriteStyleSheets();
|
|
218
236
|
}); // IE doesn't support shadow DOM
|
|
219
237
|
|
|
220
238
|
|
|
@@ -27,8 +27,8 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
27
27
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
28
28
|
|
|
29
29
|
/* eslint-env browser */
|
|
30
|
-
import { addons, useEffect, useGlobals
|
|
31
|
-
import { STORY_CHANGED, STORY_RENDERED } from "@storybook/core-events";
|
|
30
|
+
import { addons, useEffect, useGlobals } from "@storybook/addons";
|
|
31
|
+
import { DOCS_RENDERED, STORY_CHANGED, STORY_RENDERED } from "@storybook/core-events";
|
|
32
32
|
import { PSEUDO_STATES } from "./constants";
|
|
33
33
|
var pseudoStates = Object.values(PSEUDO_STATES);
|
|
34
34
|
var matchOne = new RegExp(":(".concat(pseudoStates.join("|"), ")"));
|
|
@@ -41,6 +41,21 @@ var applyClasses = function applyClasses(element, classnames) {
|
|
|
41
41
|
element.className = (_element$className$sp = element.className.split(" ").filter(function (classname) {
|
|
42
42
|
return classname && classname.indexOf("pseudo-") !== 0;
|
|
43
43
|
})).concat.apply(_element$className$sp, _toConsumableArray(classnames)).join(" ");
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
var applyParameter = function applyParameter(element, parameter) {
|
|
47
|
+
return applyClasses(element, Object.entries(parameter || {}).filter(function (_ref) {
|
|
48
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
49
|
+
_ = _ref2[0],
|
|
50
|
+
value = _ref2[1];
|
|
51
|
+
|
|
52
|
+
return value;
|
|
53
|
+
}).map(function (_ref3) {
|
|
54
|
+
var _ref4 = _slicedToArray(_ref3, 1),
|
|
55
|
+
key = _ref4[0];
|
|
56
|
+
|
|
57
|
+
return "pseudo-".concat(PSEUDO_STATES[key]);
|
|
58
|
+
}));
|
|
44
59
|
}; // Traverses ancestry to collect relevant pseudo classnames, and applies them to the shadow host.
|
|
45
60
|
// Shadow DOM can only access classes on its host. Traversing is needed to mimic the CSS cascade.
|
|
46
61
|
|
|
@@ -66,37 +81,35 @@ addons.getChannel().on(STORY_CHANGED, function () {
|
|
|
66
81
|
return shadowHosts.clear();
|
|
67
82
|
}); // Global decorator that rewrites stylesheets and applies classnames to render pseudo styles
|
|
68
83
|
|
|
69
|
-
export var withPseudoState = function withPseudoState(StoryFn) {
|
|
70
|
-
var
|
|
84
|
+
export var withPseudoState = function withPseudoState(StoryFn, _ref5) {
|
|
85
|
+
var viewMode = _ref5.viewMode,
|
|
86
|
+
parameters = _ref5.parameters,
|
|
87
|
+
id = _ref5.id;
|
|
88
|
+
var parameter = parameters.pseudo;
|
|
71
89
|
|
|
72
90
|
var _useGlobals = useGlobals(),
|
|
73
91
|
_useGlobals2 = _slicedToArray(_useGlobals, 2),
|
|
74
92
|
globals = _useGlobals2[0].pseudo,
|
|
75
|
-
updateGlobals = _useGlobals2[1]; // Sync parameter to globals, used by the toolbar
|
|
93
|
+
updateGlobals = _useGlobals2[1]; // Sync parameter to globals, used by the toolbar (only in canvas as this
|
|
94
|
+
// doesn't make sense for docs because many stories are displayed at once)
|
|
76
95
|
|
|
77
96
|
|
|
78
97
|
useEffect(function () {
|
|
79
|
-
if (parameter !== globals) updateGlobals({
|
|
98
|
+
if (parameter !== globals && viewMode === "story") updateGlobals({
|
|
80
99
|
pseudo: parameter
|
|
81
100
|
});
|
|
82
|
-
}, [parameter]); // Convert selected states to classnames and apply them to the story root element.
|
|
101
|
+
}, [parameter, viewMode]); // Convert selected states to classnames and apply them to the story root element.
|
|
83
102
|
// Then update each shadow host to redetermine its own pseudo classnames.
|
|
84
103
|
|
|
85
104
|
useEffect(function () {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
key = _ref4[0];
|
|
95
|
-
|
|
96
|
-
return "pseudo-".concat(PSEUDO_STATES[key]);
|
|
97
|
-
}));
|
|
98
|
-
shadowHosts.forEach(updateShadowHost);
|
|
99
|
-
}, [globals]);
|
|
105
|
+
var timeout = setTimeout(function () {
|
|
106
|
+
applyParameter(document.getElementById(viewMode === "docs" ? "story--".concat(id) : "root"));
|
|
107
|
+
shadowHosts.forEach(updateShadowHost);
|
|
108
|
+
}, 0);
|
|
109
|
+
return function () {
|
|
110
|
+
return clearTimeout(timeout);
|
|
111
|
+
};
|
|
112
|
+
}, [parameter, viewMode]);
|
|
100
113
|
return StoryFn();
|
|
101
114
|
};
|
|
102
115
|
var warnings = new Set();
|
|
@@ -202,6 +215,10 @@ function rewriteStyleSheets(shadowRoot) {
|
|
|
202
215
|
|
|
203
216
|
addons.getChannel().on(STORY_RENDERED, function () {
|
|
204
217
|
return rewriteStyleSheets();
|
|
218
|
+
}); // Reinitialize CSS enhancements every time a docs page is rendered
|
|
219
|
+
|
|
220
|
+
addons.getChannel().on(DOCS_RENDERED, function () {
|
|
221
|
+
return rewriteStyleSheets();
|
|
205
222
|
}); // IE doesn't support shadow DOM
|
|
206
223
|
|
|
207
224
|
if (Element.prototype.attachShadow) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "storybook-addon-pseudo-states",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3--canary.d752c59.0",
|
|
4
4
|
"description": "CSS pseudo states for Storybook",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"storybook-addons",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"@babel/core": "^7.12.3",
|
|
38
38
|
"@babel/preset-env": "^7.12.1",
|
|
39
39
|
"@babel/preset-react": "^7.12.5",
|
|
40
|
+
"@storybook/addon-docs": "^6.4.19",
|
|
40
41
|
"@storybook/react": "^6.4.19",
|
|
41
42
|
"auto": "^10.16.8",
|
|
42
43
|
"babel-loader": "^8.1.0",
|