@vueless/storybook-dark-mode 9.0.10 → 10.0.1-beta.1

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/dist/esm/Tool.js DELETED
@@ -1,242 +0,0 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- var _excluded = ["current", "stylePreview"];
3
- var _window$matchMedia;
4
- function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
5
- function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
6
- function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
7
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
8
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
9
- function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
10
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
11
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
12
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
13
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
14
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
15
- function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
16
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
17
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
18
- function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
19
- function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
20
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
21
- import * as React from 'react';
22
- import { global } from '@storybook/global';
23
- import { themes } from 'storybook/theming';
24
- import { IconButton } from 'storybook/internal/components';
25
- import { MoonIcon, SunIcon } from '@storybook/icons';
26
- import { STORY_CHANGED, SET_STORIES, DOCS_RENDERED } from 'storybook/internal/core-events';
27
- import { useParameter } from 'storybook/manager-api';
28
- import equal from 'fast-deep-equal';
29
- import { DARK_MODE_EVENT_NAME, UPDATE_DARK_MODE_EVENT_NAME } from './constants';
30
- var _ref = global,
31
- document = _ref.document,
32
- window = _ref.window;
33
- var modes = ['light', 'dark'];
34
- var STORAGE_KEY = 'sb-addon-themes-3';
35
- export var prefersDark = (_window$matchMedia = window.matchMedia) === null || _window$matchMedia === void 0 ? void 0 : _window$matchMedia.call(window, '(prefers-color-scheme: dark)');
36
- var defaultParams = {
37
- classTarget: 'body',
38
- dark: themes.dark,
39
- darkClass: ['dark'],
40
- light: themes.light,
41
- lightClass: ['light'],
42
- stylePreview: false,
43
- userHasExplicitlySetTheTheme: false
44
- };
45
-
46
- /** Persist the dark mode settings in localStorage */
47
- export var updateStore = function updateStore(newStore) {
48
- window.localStorage.setItem(STORAGE_KEY, JSON.stringify(newStore));
49
- };
50
-
51
- /** Add the light/dark class to an element */
52
- var toggleDarkClass = function toggleDarkClass(el, _ref2) {
53
- var current = _ref2.current,
54
- _ref2$darkClass = _ref2.darkClass,
55
- darkClass = _ref2$darkClass === void 0 ? defaultParams.darkClass : _ref2$darkClass,
56
- _ref2$lightClass = _ref2.lightClass,
57
- lightClass = _ref2$lightClass === void 0 ? defaultParams.lightClass : _ref2$lightClass;
58
- if (current === 'dark') {
59
- var _el$classList, _el$classList2;
60
- (_el$classList = el.classList).remove.apply(_el$classList, _toConsumableArray(arrayify(lightClass)));
61
- (_el$classList2 = el.classList).add.apply(_el$classList2, _toConsumableArray(arrayify(darkClass)));
62
- } else {
63
- var _el$classList3, _el$classList4;
64
- (_el$classList3 = el.classList).remove.apply(_el$classList3, _toConsumableArray(arrayify(darkClass)));
65
- (_el$classList4 = el.classList).add.apply(_el$classList4, _toConsumableArray(arrayify(lightClass)));
66
- }
67
- };
68
-
69
- /** Coerce a string to a single item array, or return an array as-is */
70
- var arrayify = function arrayify(classes) {
71
- var arr = [];
72
- return arr.concat(classes).map(function (item) {
73
- return item;
74
- });
75
- };
76
-
77
- /** Update the preview iframe class */
78
- var updatePreview = function updatePreview(store) {
79
- var _iframe$contentWindow;
80
- var iframe = document.getElementById('storybook-preview-iframe');
81
- if (!iframe) {
82
- return;
83
- }
84
- var iframeDocument = iframe.contentDocument || ((_iframe$contentWindow = iframe.contentWindow) === null || _iframe$contentWindow === void 0 ? void 0 : _iframe$contentWindow.document);
85
- var target = iframeDocument === null || iframeDocument === void 0 ? void 0 : iframeDocument.querySelector(store.classTarget);
86
- if (!target) {
87
- return;
88
- }
89
- toggleDarkClass(target, store);
90
- };
91
-
92
- /** Update the manager iframe class */
93
- var updateManager = function updateManager(store) {
94
- var manager = document.querySelector(store.classTarget);
95
- if (!manager) {
96
- return;
97
- }
98
- toggleDarkClass(manager, store);
99
- };
100
-
101
- /** Update changed dark mode settings and persist to localStorage */
102
- export var store = function store() {
103
- var userTheme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
104
- var storedItem = window.localStorage.getItem(STORAGE_KEY);
105
- if (typeof storedItem === 'string') {
106
- var stored = JSON.parse(storedItem);
107
- if (userTheme) {
108
- if (userTheme.dark && !equal(stored.dark, userTheme.dark)) {
109
- stored.dark = userTheme.dark;
110
- updateStore(stored);
111
- }
112
- if (userTheme.light && !equal(stored.light, userTheme.light)) {
113
- stored.light = userTheme.light;
114
- updateStore(stored);
115
- }
116
- }
117
- return stored;
118
- }
119
- return _objectSpread(_objectSpread({}, defaultParams), userTheme);
120
- };
121
-
122
- // On initial load, set the dark mode class on the manager
123
- // This is needed if you're using mostly CSS overrides to styles the storybook
124
- // Otherwise the default theme is set in src/preset/manager.tsx
125
- updateManager(store());
126
- /** A toolbar icon to toggle between dark and light themes in storybook */
127
- export function DarkMode(_ref3) {
128
- var api = _ref3.api;
129
- var _React$useState = React.useState(prefersDark.matches),
130
- _React$useState2 = _slicedToArray(_React$useState, 2),
131
- isDark = _React$useState2[0],
132
- setDark = _React$useState2[1];
133
- var darkModeParams = useParameter('darkMode', {});
134
- var defaultMode = darkModeParams.current,
135
- stylePreview = darkModeParams.stylePreview,
136
- params = _objectWithoutProperties(darkModeParams, _excluded);
137
- var channel = api.getChannel();
138
- // Save custom themes on init
139
- var userHasExplicitlySetTheTheme = React.useMemo(function () {
140
- return store(params).userHasExplicitlySetTheTheme;
141
- }, [params]);
142
- /** Set the theme in storybook, update the local state, and emit an event */
143
- var setMode = React.useCallback(function (mode) {
144
- var currentStore = store();
145
- api.setOptions({
146
- theme: currentStore[mode]
147
- });
148
- setDark(mode === 'dark');
149
- api.getChannel().emit(DARK_MODE_EVENT_NAME, mode === 'dark');
150
- updateManager(currentStore);
151
- if (stylePreview) {
152
- updatePreview(currentStore);
153
- }
154
- }, [api, stylePreview]);
155
-
156
- /** Update the theme settings in localStorage, react, and storybook */
157
- var updateMode = React.useCallback(function (mode) {
158
- var currentStore = store();
159
- var current = mode || (currentStore.current === 'dark' ? 'light' : 'dark');
160
- updateStore(_objectSpread(_objectSpread({}, currentStore), {}, {
161
- current: current
162
- }));
163
- setMode(current);
164
- }, [setMode]);
165
-
166
- /** Update the theme based on the color preference */
167
- function prefersDarkUpdate(event) {
168
- if (userHasExplicitlySetTheTheme || defaultMode) {
169
- return;
170
- }
171
- updateMode(event.matches ? 'dark' : 'light');
172
- }
173
-
174
- /** Render the current theme */
175
- var renderTheme = React.useCallback(function () {
176
- var _store = store(),
177
- _store$current = _store.current,
178
- current = _store$current === void 0 ? 'light' : _store$current;
179
- setMode(current);
180
- }, [setMode]);
181
-
182
- /** Handle the user event and side effects */
183
- var handleIconClick = function handleIconClick() {
184
- updateMode();
185
- var currentStore = store();
186
- updateStore(_objectSpread(_objectSpread({}, currentStore), {}, {
187
- userHasExplicitlySetTheTheme: true
188
- }));
189
- };
190
-
191
- /** When storybook params change update the stored themes */
192
- React.useEffect(function () {
193
- var currentStore = store();
194
- // Ensure we use the stores `current` value first to persist
195
- // themeing between page loads and story changes.
196
- updateStore(_objectSpread(_objectSpread(_objectSpread({}, currentStore), darkModeParams), {}, {
197
- current: currentStore.current || darkModeParams.current
198
- }));
199
- renderTheme();
200
- }, [darkModeParams, renderTheme]);
201
- React.useEffect(function () {
202
- channel.on(STORY_CHANGED, renderTheme);
203
- channel.on(SET_STORIES, renderTheme);
204
- channel.on(DOCS_RENDERED, renderTheme);
205
- prefersDark.addListener(prefersDarkUpdate);
206
- return function () {
207
- channel.removeListener(STORY_CHANGED, renderTheme);
208
- channel.removeListener(SET_STORIES, renderTheme);
209
- channel.removeListener(DOCS_RENDERED, renderTheme);
210
- prefersDark.removeListener(prefersDarkUpdate);
211
- };
212
- });
213
- React.useEffect(function () {
214
- channel.on(UPDATE_DARK_MODE_EVENT_NAME, updateMode);
215
- return function () {
216
- channel.removeListener(UPDATE_DARK_MODE_EVENT_NAME, updateMode);
217
- };
218
- });
219
- // Storybook's first render doesn't have the global user params loaded so we
220
- // need the effect to run whenever defaultMode is updated
221
- React.useEffect(function () {
222
- // If a users has set the mode this is respected
223
- if (userHasExplicitlySetTheTheme) {
224
- return;
225
- }
226
- if (defaultMode) {
227
- updateMode(defaultMode);
228
- } else if (prefersDark.matches) {
229
- updateMode('dark');
230
- }
231
- }, [defaultMode, updateMode, userHasExplicitlySetTheTheme]);
232
- return /*#__PURE__*/React.createElement(IconButton, {
233
- key: "dark-mode",
234
- title: isDark ? 'Change theme to light mode' : 'Change theme to dark mode',
235
- onClick: handleIconClick
236
- }, isDark ? /*#__PURE__*/React.createElement(SunIcon, {
237
- "aria-hidden": "true"
238
- }) : /*#__PURE__*/React.createElement(MoonIcon, {
239
- "aria-hidden": "true"
240
- }));
241
- }
242
- export default DarkMode;
@@ -1,2 +0,0 @@
1
- export var DARK_MODE_EVENT_NAME = 'DARK_MODE';
2
- export var UPDATE_DARK_MODE_EVENT_NAME = 'UPDATE_DARK_MODE';
package/dist/esm/index.js DELETED
@@ -1,30 +0,0 @@
1
- function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
2
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
4
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
5
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
- function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
7
- import { addons, useState, useEffect } from 'storybook/preview-api';
8
- import { DARK_MODE_EVENT_NAME } from './constants';
9
- import { store } from './Tool';
10
-
11
- /**
12
- * Returns the current state of storybook's dark-mode
13
- */
14
- export function useDarkMode() {
15
- var _useState = useState(function () {
16
- return store().current === 'dark';
17
- }),
18
- _useState2 = _slicedToArray(_useState, 2),
19
- isDark = _useState2[0],
20
- setIsDark = _useState2[1];
21
- useEffect(function () {
22
- var chan = addons.getChannel();
23
- chan.on(DARK_MODE_EVENT_NAME, setIsDark);
24
- return function () {
25
- return chan.off(DARK_MODE_EVENT_NAME, setIsDark);
26
- };
27
- }, []);
28
- return isDark;
29
- }
30
- export * from './constants';
@@ -1,31 +0,0 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
5
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
6
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
- import { addons } from 'storybook/manager-api';
8
- import { Addon_TypesEnum } from 'storybook/internal/types';
9
- import { themes } from 'storybook/theming';
10
- import * as React from 'react';
11
- import Tool, { prefersDark, store } from '../Tool';
12
- var currentStore = store();
13
- var currentTheme = currentStore.current || prefersDark.matches && 'dark' || 'light';
14
- addons.setConfig({
15
- theme: _objectSpread(_objectSpread({}, themes[currentTheme]), currentStore[currentTheme])
16
- });
17
- addons.register('storybook/dark-mode', function (api) {
18
- addons.add('storybook/dark-mode', {
19
- title: 'dark mode',
20
- type: Addon_TypesEnum.TOOL,
21
- match: function match(_ref) {
22
- var viewMode = _ref.viewMode;
23
- return viewMode === 'story' || viewMode === 'docs';
24
- },
25
- render: function render() {
26
- return /*#__PURE__*/React.createElement(Tool, {
27
- api: api
28
- });
29
- }
30
- });
31
- });
package/dist/ts/Tool.d.ts DELETED
@@ -1,35 +0,0 @@
1
- import * as React from 'react';
2
- import { ThemeVars } from 'storybook/theming';
3
- import { API } from 'storybook/manager-api';
4
- declare const modes: readonly ["light", "dark"];
5
- type Mode = typeof modes[number];
6
- interface DarkModeStore {
7
- /** The class target in the preview iframe */
8
- classTarget: string;
9
- /** The current mode the storybook is set to */
10
- current: Mode;
11
- /** The dark theme for storybook */
12
- dark: ThemeVars;
13
- /** The dark class name for the preview iframe */
14
- darkClass: string | string[];
15
- /** The light theme for storybook */
16
- light: ThemeVars;
17
- /** The light class name for the preview iframe */
18
- lightClass: string | string[];
19
- /** Apply mode to iframe */
20
- stylePreview: boolean;
21
- /** Persist if the user has set the theme */
22
- userHasExplicitlySetTheTheme: boolean;
23
- }
24
- export declare const prefersDark: MediaQueryList;
25
- /** Persist the dark mode settings in localStorage */
26
- export declare const updateStore: (newStore: DarkModeStore) => void;
27
- /** Update changed dark mode settings and persist to localStorage */
28
- export declare const store: (userTheme?: Partial<DarkModeStore>) => DarkModeStore;
29
- interface DarkModeProps {
30
- /** The storybook API */
31
- api: API;
32
- }
33
- /** A toolbar icon to toggle between dark and light themes in storybook */
34
- export declare function DarkMode({ api }: DarkModeProps): React.JSX.Element;
35
- export default DarkMode;
@@ -1,2 +0,0 @@
1
- export declare const DARK_MODE_EVENT_NAME = "DARK_MODE";
2
- export declare const UPDATE_DARK_MODE_EVENT_NAME = "UPDATE_DARK_MODE";
@@ -1,5 +0,0 @@
1
- /**
2
- * Returns the current state of storybook's dark-mode
3
- */
4
- export declare function useDarkMode(): boolean;
5
- export * from './constants';
@@ -1 +0,0 @@
1
- export {};