@storybook/addon-backgrounds 7.0.0-alpha.5 → 7.0.0-alpha.51
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/README.md +1 -1
- package/dist/chunk-6BSBHII6.mjs +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/manager.d.ts +1 -0
- package/dist/manager.js +5 -0
- package/dist/manager.mjs +1 -0
- package/dist/preview.d.ts +21 -0
- package/dist/preview.js +20 -0
- package/dist/preview.mjs +16 -0
- package/jest.config.js +7 -0
- package/manager.js +1 -1
- package/package.json +61 -16
- package/preview.js +1 -1
- package/LICENSE +0 -21
- package/dist/cjs/components/ColorIcon.js +0 -24
- package/dist/cjs/constants.js +0 -16
- package/dist/cjs/containers/BackgroundSelector.js +0 -115
- package/dist/cjs/containers/GridSelector.js +0 -50
- package/dist/cjs/decorators/withBackground.js +0 -54
- package/dist/cjs/decorators/withGrid.js +0 -84
- package/dist/cjs/helpers/index.js +0 -113
- package/dist/cjs/index.js +0 -14
- package/dist/cjs/manager.js +0 -26
- package/dist/cjs/preview.js +0 -30
- package/dist/cjs/types/index.js +0 -5
- package/dist/esm/components/ColorIcon.js +0 -14
- package/dist/esm/constants.js +0 -6
- package/dist/esm/containers/BackgroundSelector.js +0 -91
- package/dist/esm/containers/GridSelector.js +0 -32
- package/dist/esm/decorators/withBackground.js +0 -40
- package/dist/esm/decorators/withGrid.js +0 -65
- package/dist/esm/helpers/index.js +0 -86
- package/dist/esm/index.js +0 -6
- package/dist/esm/manager.js +0 -15
- package/dist/esm/preview.js +0 -19
- package/dist/esm/types/index.js +0 -1
- package/dist/types/components/ColorIcon.d.ts +0 -3
- package/dist/types/constants.d.ts +0 -6
- package/dist/types/containers/BackgroundSelector.d.ts +0 -2
- package/dist/types/containers/GridSelector.d.ts +0 -2
- package/dist/types/decorators/withBackground.d.ts +0 -2
- package/dist/types/decorators/withGrid.d.ts +0 -2
- package/dist/types/helpers/index.d.ts +0 -6
- package/dist/types/index.d.ts +0 -2
- package/dist/types/manager.d.ts +0 -1
- package/dist/types/preview.d.ts +0 -14
- package/dist/types/types/index.d.ts +0 -28
- package/register.js +0 -6
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.isReduceMotionEnabled = exports.getBackgroundColorByName = exports.clearStyles = exports.addGridStyle = exports.addBackgroundStyle = void 0;
|
|
7
|
-
|
|
8
|
-
var _global = _interopRequireDefault(require("global"));
|
|
9
|
-
|
|
10
|
-
var _tsDedent = _interopRequireDefault(require("ts-dedent"));
|
|
11
|
-
|
|
12
|
-
var _clientLogger = require("@storybook/client-logger");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
const {
|
|
17
|
-
document,
|
|
18
|
-
window
|
|
19
|
-
} = _global.default;
|
|
20
|
-
|
|
21
|
-
const isReduceMotionEnabled = () => {
|
|
22
|
-
const prefersReduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
23
|
-
return prefersReduceMotion.matches;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
exports.isReduceMotionEnabled = isReduceMotionEnabled;
|
|
27
|
-
|
|
28
|
-
const getBackgroundColorByName = (currentSelectedValue, backgrounds = [], defaultName) => {
|
|
29
|
-
if (currentSelectedValue === 'transparent') {
|
|
30
|
-
return 'transparent';
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (backgrounds.find(background => background.value === currentSelectedValue)) {
|
|
34
|
-
return currentSelectedValue;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const defaultBackground = backgrounds.find(background => background.name === defaultName);
|
|
38
|
-
|
|
39
|
-
if (defaultBackground) {
|
|
40
|
-
return defaultBackground.value;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (defaultName) {
|
|
44
|
-
const availableColors = backgrounds.map(background => background.name).join(', ');
|
|
45
|
-
|
|
46
|
-
_clientLogger.logger.warn((0, _tsDedent.default)`
|
|
47
|
-
Backgrounds Addon: could not find the default color "${defaultName}".
|
|
48
|
-
These are the available colors for your story based on your configuration:
|
|
49
|
-
${availableColors}.
|
|
50
|
-
`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return 'transparent';
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
exports.getBackgroundColorByName = getBackgroundColorByName;
|
|
57
|
-
|
|
58
|
-
const clearStyles = selector => {
|
|
59
|
-
const selectors = Array.isArray(selector) ? selector : [selector];
|
|
60
|
-
selectors.forEach(clearStyle);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
exports.clearStyles = clearStyles;
|
|
64
|
-
|
|
65
|
-
const clearStyle = selector => {
|
|
66
|
-
const element = document.getElementById(selector);
|
|
67
|
-
|
|
68
|
-
if (element) {
|
|
69
|
-
element.parentElement.removeChild(element);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const addGridStyle = (selector, css) => {
|
|
74
|
-
const existingStyle = document.getElementById(selector);
|
|
75
|
-
|
|
76
|
-
if (existingStyle) {
|
|
77
|
-
if (existingStyle.innerHTML !== css) {
|
|
78
|
-
existingStyle.innerHTML = css;
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
const style = document.createElement('style');
|
|
82
|
-
style.setAttribute('id', selector);
|
|
83
|
-
style.innerHTML = css;
|
|
84
|
-
document.head.appendChild(style);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
exports.addGridStyle = addGridStyle;
|
|
89
|
-
|
|
90
|
-
const addBackgroundStyle = (selector, css, storyId) => {
|
|
91
|
-
const existingStyle = document.getElementById(selector);
|
|
92
|
-
|
|
93
|
-
if (existingStyle) {
|
|
94
|
-
if (existingStyle.innerHTML !== css) {
|
|
95
|
-
existingStyle.innerHTML = css;
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
const style = document.createElement('style');
|
|
99
|
-
style.setAttribute('id', selector);
|
|
100
|
-
style.innerHTML = css;
|
|
101
|
-
const gridStyleSelector = `addon-backgrounds-grid${storyId ? `-docs-${storyId}` : ''}`; // If grids already exist, we want to add the style tag BEFORE it so the background doesn't override grid
|
|
102
|
-
|
|
103
|
-
const existingGridStyle = document.getElementById(gridStyleSelector);
|
|
104
|
-
|
|
105
|
-
if (existingGridStyle) {
|
|
106
|
-
existingGridStyle.parentElement.insertBefore(style, existingGridStyle);
|
|
107
|
-
} else {
|
|
108
|
-
document.head.appendChild(style);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
exports.addBackgroundStyle = addBackgroundStyle;
|
package/dist/cjs/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
if (module && module.hot && module.hot.decline) {
|
|
9
|
-
module.hot.decline();
|
|
10
|
-
} // make it work with --isolatedModules
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var _default = {};
|
|
14
|
-
exports.default = _default;
|
package/dist/cjs/manager.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _react = _interopRequireWildcard(require("react"));
|
|
4
|
-
|
|
5
|
-
var _addons = require("@storybook/addons");
|
|
6
|
-
|
|
7
|
-
var _constants = require("./constants");
|
|
8
|
-
|
|
9
|
-
var _BackgroundSelector = require("./containers/BackgroundSelector");
|
|
10
|
-
|
|
11
|
-
var _GridSelector = require("./containers/GridSelector");
|
|
12
|
-
|
|
13
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
|
-
|
|
15
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
16
|
-
|
|
17
|
-
_addons.addons.register(_constants.ADDON_ID, () => {
|
|
18
|
-
_addons.addons.add(_constants.ADDON_ID, {
|
|
19
|
-
title: 'Backgrounds',
|
|
20
|
-
type: _addons.types.TOOL,
|
|
21
|
-
match: ({
|
|
22
|
-
viewMode
|
|
23
|
-
}) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
|
|
24
|
-
render: () => /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(_BackgroundSelector.BackgroundSelector, null), /*#__PURE__*/_react.default.createElement(_GridSelector.GridSelector, null))
|
|
25
|
-
});
|
|
26
|
-
});
|
package/dist/cjs/preview.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.parameters = exports.decorators = void 0;
|
|
7
|
-
|
|
8
|
-
var _withBackground = require("./decorators/withBackground");
|
|
9
|
-
|
|
10
|
-
var _withGrid = require("./decorators/withGrid");
|
|
11
|
-
|
|
12
|
-
const decorators = [_withGrid.withGrid, _withBackground.withBackground];
|
|
13
|
-
exports.decorators = decorators;
|
|
14
|
-
const parameters = {
|
|
15
|
-
backgrounds: {
|
|
16
|
-
grid: {
|
|
17
|
-
cellSize: 20,
|
|
18
|
-
opacity: 0.5,
|
|
19
|
-
cellAmount: 5
|
|
20
|
-
},
|
|
21
|
-
values: [{
|
|
22
|
-
name: 'light',
|
|
23
|
-
value: '#F8F8F8'
|
|
24
|
-
}, {
|
|
25
|
-
name: 'dark',
|
|
26
|
-
value: '#333333'
|
|
27
|
-
}]
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
exports.parameters = parameters;
|
package/dist/cjs/types/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { styled } from '@storybook/theming';
|
|
2
|
-
export const ColorIcon = styled.span(({
|
|
3
|
-
background
|
|
4
|
-
}) => ({
|
|
5
|
-
borderRadius: '1rem',
|
|
6
|
-
display: 'block',
|
|
7
|
-
height: '1rem',
|
|
8
|
-
width: '1rem',
|
|
9
|
-
background
|
|
10
|
-
}), ({
|
|
11
|
-
theme
|
|
12
|
-
}) => ({
|
|
13
|
-
boxShadow: `${theme.appBorderColor} 0 0 0 1px inset`
|
|
14
|
-
}));
|
package/dist/esm/constants.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import React, { Fragment, useCallback, useMemo, memo } from 'react';
|
|
2
|
-
import memoize from 'memoizerific';
|
|
3
|
-
import { useParameter, useGlobals } from '@storybook/api';
|
|
4
|
-
import { logger } from '@storybook/client-logger';
|
|
5
|
-
import { Icons, IconButton, WithTooltip, TooltipLinkList } from '@storybook/components';
|
|
6
|
-
import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
|
|
7
|
-
import { ColorIcon } from '../components/ColorIcon';
|
|
8
|
-
import { getBackgroundColorByName } from '../helpers';
|
|
9
|
-
const createBackgroundSelectorItem = memoize(1000)((id, name, value, hasSwatch, change, active) => ({
|
|
10
|
-
id: id || name,
|
|
11
|
-
title: name,
|
|
12
|
-
onClick: () => {
|
|
13
|
-
change({
|
|
14
|
-
selected: value,
|
|
15
|
-
name
|
|
16
|
-
});
|
|
17
|
-
},
|
|
18
|
-
value,
|
|
19
|
-
right: hasSwatch ? /*#__PURE__*/React.createElement(ColorIcon, {
|
|
20
|
-
background: value
|
|
21
|
-
}) : undefined,
|
|
22
|
-
active
|
|
23
|
-
}));
|
|
24
|
-
const getDisplayedItems = memoize(10)((backgrounds, selectedBackgroundColor, change) => {
|
|
25
|
-
const backgroundSelectorItems = backgrounds.map(({
|
|
26
|
-
name,
|
|
27
|
-
value
|
|
28
|
-
}) => createBackgroundSelectorItem(null, name, value, true, change, value === selectedBackgroundColor));
|
|
29
|
-
|
|
30
|
-
if (selectedBackgroundColor !== 'transparent') {
|
|
31
|
-
return [createBackgroundSelectorItem('reset', 'Clear background', 'transparent', null, change, false), ...backgroundSelectorItems];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return backgroundSelectorItems;
|
|
35
|
-
});
|
|
36
|
-
const DEFAULT_BACKGROUNDS_CONFIG = {
|
|
37
|
-
default: null,
|
|
38
|
-
disable: true,
|
|
39
|
-
values: []
|
|
40
|
-
};
|
|
41
|
-
export const BackgroundSelector = /*#__PURE__*/memo(() => {
|
|
42
|
-
const backgroundsConfig = useParameter(BACKGROUNDS_PARAM_KEY, DEFAULT_BACKGROUNDS_CONFIG);
|
|
43
|
-
const [globals, updateGlobals] = useGlobals();
|
|
44
|
-
const globalsBackgroundColor = globals[BACKGROUNDS_PARAM_KEY]?.value;
|
|
45
|
-
const selectedBackgroundColor = useMemo(() => {
|
|
46
|
-
return getBackgroundColorByName(globalsBackgroundColor, backgroundsConfig.values, backgroundsConfig.default);
|
|
47
|
-
}, [backgroundsConfig, globalsBackgroundColor]);
|
|
48
|
-
|
|
49
|
-
if (Array.isArray(backgroundsConfig)) {
|
|
50
|
-
logger.warn('Addon Backgrounds api has changed in Storybook 6.0. Please refer to the migration guide: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const onBackgroundChange = useCallback(value => {
|
|
54
|
-
updateGlobals({
|
|
55
|
-
[BACKGROUNDS_PARAM_KEY]: Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
|
|
56
|
-
value
|
|
57
|
-
})
|
|
58
|
-
});
|
|
59
|
-
}, [backgroundsConfig, globals, updateGlobals]);
|
|
60
|
-
|
|
61
|
-
if (backgroundsConfig.disable) {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(WithTooltip, {
|
|
66
|
-
placement: "top",
|
|
67
|
-
trigger: "click",
|
|
68
|
-
closeOnClick: true,
|
|
69
|
-
tooltip: ({
|
|
70
|
-
onHide
|
|
71
|
-
}) => {
|
|
72
|
-
return /*#__PURE__*/React.createElement(TooltipLinkList, {
|
|
73
|
-
links: getDisplayedItems(backgroundsConfig.values, selectedBackgroundColor, ({
|
|
74
|
-
selected
|
|
75
|
-
}) => {
|
|
76
|
-
if (selectedBackgroundColor !== selected) {
|
|
77
|
-
onBackgroundChange(selected);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
onHide();
|
|
81
|
-
})
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}, /*#__PURE__*/React.createElement(IconButton, {
|
|
85
|
-
key: "background",
|
|
86
|
-
title: "Change the background of the preview",
|
|
87
|
-
active: selectedBackgroundColor !== 'transparent'
|
|
88
|
-
}, /*#__PURE__*/React.createElement(Icons, {
|
|
89
|
-
icon: "photo"
|
|
90
|
-
}))));
|
|
91
|
-
});
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import React, { memo } from 'react';
|
|
2
|
-
import { useGlobals, useParameter } from '@storybook/api';
|
|
3
|
-
import { Icons, IconButton } from '@storybook/components';
|
|
4
|
-
import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
|
|
5
|
-
export const GridSelector = /*#__PURE__*/memo(() => {
|
|
6
|
-
const [globals, updateGlobals] = useGlobals();
|
|
7
|
-
const {
|
|
8
|
-
grid
|
|
9
|
-
} = useParameter(BACKGROUNDS_PARAM_KEY, {
|
|
10
|
-
grid: {
|
|
11
|
-
disable: false
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
if (grid?.disable) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const isActive = globals[BACKGROUNDS_PARAM_KEY]?.grid || false;
|
|
20
|
-
return /*#__PURE__*/React.createElement(IconButton, {
|
|
21
|
-
key: "background",
|
|
22
|
-
active: isActive,
|
|
23
|
-
title: "Apply a grid to the preview",
|
|
24
|
-
onClick: () => updateGlobals({
|
|
25
|
-
[BACKGROUNDS_PARAM_KEY]: Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
|
|
26
|
-
grid: !isActive
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
}, /*#__PURE__*/React.createElement(Icons, {
|
|
30
|
-
icon: "grid"
|
|
31
|
-
}));
|
|
32
|
-
});
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { useMemo, useEffect } from '@storybook/addons';
|
|
2
|
-
import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
|
|
3
|
-
import { clearStyles, addBackgroundStyle, getBackgroundColorByName, isReduceMotionEnabled } from '../helpers';
|
|
4
|
-
export const withBackground = (StoryFn, context) => {
|
|
5
|
-
const {
|
|
6
|
-
globals,
|
|
7
|
-
parameters
|
|
8
|
-
} = context;
|
|
9
|
-
const globalsBackgroundColor = globals[BACKGROUNDS_PARAM_KEY]?.value;
|
|
10
|
-
const backgroundsConfig = parameters[BACKGROUNDS_PARAM_KEY];
|
|
11
|
-
const selectedBackgroundColor = useMemo(() => {
|
|
12
|
-
if (backgroundsConfig.disable) {
|
|
13
|
-
return 'transparent';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return getBackgroundColorByName(globalsBackgroundColor, backgroundsConfig.values, backgroundsConfig.default);
|
|
17
|
-
}, [backgroundsConfig, globalsBackgroundColor]);
|
|
18
|
-
const isActive = useMemo(() => selectedBackgroundColor && selectedBackgroundColor !== 'transparent', [selectedBackgroundColor]);
|
|
19
|
-
const selector = context.viewMode === 'docs' ? `#anchor--${context.id} .docs-story` : '.sb-show-main';
|
|
20
|
-
const backgroundStyles = useMemo(() => {
|
|
21
|
-
const transitionStyle = 'transition: background-color 0.3s;';
|
|
22
|
-
return `
|
|
23
|
-
${selector} {
|
|
24
|
-
background: ${selectedBackgroundColor} !important;
|
|
25
|
-
${isReduceMotionEnabled() ? '' : transitionStyle}
|
|
26
|
-
}
|
|
27
|
-
`;
|
|
28
|
-
}, [selectedBackgroundColor, selector]);
|
|
29
|
-
useEffect(() => {
|
|
30
|
-
const selectorId = context.viewMode === 'docs' ? `addon-backgrounds-docs-${context.id}` : `addon-backgrounds-color`;
|
|
31
|
-
|
|
32
|
-
if (!isActive) {
|
|
33
|
-
clearStyles(selectorId);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
addBackgroundStyle(selectorId, backgroundStyles, context.viewMode === 'docs' ? context.id : null);
|
|
38
|
-
}, [isActive, backgroundStyles, context]);
|
|
39
|
-
return StoryFn();
|
|
40
|
-
};
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import dedent from 'ts-dedent';
|
|
2
|
-
import deprecate from 'util-deprecate';
|
|
3
|
-
import { useMemo, useEffect } from '@storybook/addons';
|
|
4
|
-
import { clearStyles, addGridStyle } from '../helpers';
|
|
5
|
-
import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
|
|
6
|
-
const deprecatedCellSizeWarning = deprecate(() => {}, dedent`
|
|
7
|
-
Backgrounds Addon: The cell size parameter has been changed.
|
|
8
|
-
|
|
9
|
-
- parameters.grid.cellSize should now be parameters.backgrounds.grid.cellSize
|
|
10
|
-
See https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-grid-parameter
|
|
11
|
-
`);
|
|
12
|
-
export const withGrid = (StoryFn, context) => {
|
|
13
|
-
const {
|
|
14
|
-
globals,
|
|
15
|
-
parameters
|
|
16
|
-
} = context;
|
|
17
|
-
const gridParameters = parameters[BACKGROUNDS_PARAM_KEY].grid;
|
|
18
|
-
const isActive = globals[BACKGROUNDS_PARAM_KEY]?.grid === true && gridParameters.disable !== true;
|
|
19
|
-
const {
|
|
20
|
-
cellAmount,
|
|
21
|
-
cellSize,
|
|
22
|
-
opacity
|
|
23
|
-
} = gridParameters;
|
|
24
|
-
const isInDocs = context.viewMode === 'docs';
|
|
25
|
-
let gridSize;
|
|
26
|
-
|
|
27
|
-
if (parameters.grid?.cellSize) {
|
|
28
|
-
gridSize = parameters.grid.cellSize;
|
|
29
|
-
deprecatedCellSizeWarning();
|
|
30
|
-
} else {
|
|
31
|
-
gridSize = cellSize;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const isLayoutPadded = parameters.layout === undefined || parameters.layout === 'padded'; // 16px offset in the grid to account for padded layout
|
|
35
|
-
|
|
36
|
-
const defaultOffset = isLayoutPadded ? 16 : 0;
|
|
37
|
-
const offsetX = gridParameters.offsetX ?? (isInDocs ? 20 : defaultOffset);
|
|
38
|
-
const offsetY = gridParameters.offsetY ?? (isInDocs ? 20 : defaultOffset);
|
|
39
|
-
const gridStyles = useMemo(() => {
|
|
40
|
-
const selector = context.viewMode === 'docs' ? `#anchor--${context.id} .docs-story` : '.sb-show-main';
|
|
41
|
-
const backgroundSize = [`${gridSize * cellAmount}px ${gridSize * cellAmount}px`, `${gridSize * cellAmount}px ${gridSize * cellAmount}px`, `${gridSize}px ${gridSize}px`, `${gridSize}px ${gridSize}px`].join(', ');
|
|
42
|
-
return `
|
|
43
|
-
${selector} {
|
|
44
|
-
background-size: ${backgroundSize} !important;
|
|
45
|
-
background-position: ${offsetX}px ${offsetY}px, ${offsetX}px ${offsetY}px, ${offsetX}px ${offsetY}px, ${offsetX}px ${offsetY}px !important;
|
|
46
|
-
background-blend-mode: difference !important;
|
|
47
|
-
background-image: linear-gradient(rgba(130, 130, 130, ${opacity}) 1px, transparent 1px),
|
|
48
|
-
linear-gradient(90deg, rgba(130, 130, 130, ${opacity}) 1px, transparent 1px),
|
|
49
|
-
linear-gradient(rgba(130, 130, 130, ${opacity / 2}) 1px, transparent 1px),
|
|
50
|
-
linear-gradient(90deg, rgba(130, 130, 130, ${opacity / 2}) 1px, transparent 1px) !important;
|
|
51
|
-
}
|
|
52
|
-
`;
|
|
53
|
-
}, [gridSize]);
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
const selectorId = context.viewMode === 'docs' ? `addon-backgrounds-grid-docs-${context.id}` : `addon-backgrounds-grid`;
|
|
56
|
-
|
|
57
|
-
if (!isActive) {
|
|
58
|
-
clearStyles(selectorId);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
addGridStyle(selectorId, gridStyles);
|
|
63
|
-
}, [isActive, gridStyles, context]);
|
|
64
|
-
return StoryFn();
|
|
65
|
-
};
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import global from 'global';
|
|
2
|
-
import dedent from 'ts-dedent';
|
|
3
|
-
import { logger } from '@storybook/client-logger';
|
|
4
|
-
const {
|
|
5
|
-
document,
|
|
6
|
-
window
|
|
7
|
-
} = global;
|
|
8
|
-
export const isReduceMotionEnabled = () => {
|
|
9
|
-
const prefersReduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
10
|
-
return prefersReduceMotion.matches;
|
|
11
|
-
};
|
|
12
|
-
export const getBackgroundColorByName = (currentSelectedValue, backgrounds = [], defaultName) => {
|
|
13
|
-
if (currentSelectedValue === 'transparent') {
|
|
14
|
-
return 'transparent';
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (backgrounds.find(background => background.value === currentSelectedValue)) {
|
|
18
|
-
return currentSelectedValue;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const defaultBackground = backgrounds.find(background => background.name === defaultName);
|
|
22
|
-
|
|
23
|
-
if (defaultBackground) {
|
|
24
|
-
return defaultBackground.value;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (defaultName) {
|
|
28
|
-
const availableColors = backgrounds.map(background => background.name).join(', ');
|
|
29
|
-
logger.warn(dedent`
|
|
30
|
-
Backgrounds Addon: could not find the default color "${defaultName}".
|
|
31
|
-
These are the available colors for your story based on your configuration:
|
|
32
|
-
${availableColors}.
|
|
33
|
-
`);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return 'transparent';
|
|
37
|
-
};
|
|
38
|
-
export const clearStyles = selector => {
|
|
39
|
-
const selectors = Array.isArray(selector) ? selector : [selector];
|
|
40
|
-
selectors.forEach(clearStyle);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const clearStyle = selector => {
|
|
44
|
-
const element = document.getElementById(selector);
|
|
45
|
-
|
|
46
|
-
if (element) {
|
|
47
|
-
element.parentElement.removeChild(element);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const addGridStyle = (selector, css) => {
|
|
52
|
-
const existingStyle = document.getElementById(selector);
|
|
53
|
-
|
|
54
|
-
if (existingStyle) {
|
|
55
|
-
if (existingStyle.innerHTML !== css) {
|
|
56
|
-
existingStyle.innerHTML = css;
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
const style = document.createElement('style');
|
|
60
|
-
style.setAttribute('id', selector);
|
|
61
|
-
style.innerHTML = css;
|
|
62
|
-
document.head.appendChild(style);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
export const addBackgroundStyle = (selector, css, storyId) => {
|
|
66
|
-
const existingStyle = document.getElementById(selector);
|
|
67
|
-
|
|
68
|
-
if (existingStyle) {
|
|
69
|
-
if (existingStyle.innerHTML !== css) {
|
|
70
|
-
existingStyle.innerHTML = css;
|
|
71
|
-
}
|
|
72
|
-
} else {
|
|
73
|
-
const style = document.createElement('style');
|
|
74
|
-
style.setAttribute('id', selector);
|
|
75
|
-
style.innerHTML = css;
|
|
76
|
-
const gridStyleSelector = `addon-backgrounds-grid${storyId ? `-docs-${storyId}` : ''}`; // If grids already exist, we want to add the style tag BEFORE it so the background doesn't override grid
|
|
77
|
-
|
|
78
|
-
const existingGridStyle = document.getElementById(gridStyleSelector);
|
|
79
|
-
|
|
80
|
-
if (existingGridStyle) {
|
|
81
|
-
existingGridStyle.parentElement.insertBefore(style, existingGridStyle);
|
|
82
|
-
} else {
|
|
83
|
-
document.head.appendChild(style);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
};
|
package/dist/esm/index.js
DELETED
package/dist/esm/manager.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import React, { Fragment } from 'react';
|
|
2
|
-
import { addons, types } from '@storybook/addons';
|
|
3
|
-
import { ADDON_ID } from './constants';
|
|
4
|
-
import { BackgroundSelector } from './containers/BackgroundSelector';
|
|
5
|
-
import { GridSelector } from './containers/GridSelector';
|
|
6
|
-
addons.register(ADDON_ID, () => {
|
|
7
|
-
addons.add(ADDON_ID, {
|
|
8
|
-
title: 'Backgrounds',
|
|
9
|
-
type: types.TOOL,
|
|
10
|
-
match: ({
|
|
11
|
-
viewMode
|
|
12
|
-
}) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
|
|
13
|
-
render: () => /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(BackgroundSelector, null), /*#__PURE__*/React.createElement(GridSelector, null))
|
|
14
|
-
});
|
|
15
|
-
});
|
package/dist/esm/preview.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { withBackground } from './decorators/withBackground';
|
|
2
|
-
import { withGrid } from './decorators/withGrid';
|
|
3
|
-
export const decorators = [withGrid, withBackground];
|
|
4
|
-
export const parameters = {
|
|
5
|
-
backgrounds: {
|
|
6
|
-
grid: {
|
|
7
|
-
cellSize: 20,
|
|
8
|
-
opacity: 0.5,
|
|
9
|
-
cellAmount: 5
|
|
10
|
-
},
|
|
11
|
-
values: [{
|
|
12
|
-
name: 'light',
|
|
13
|
-
value: '#F8F8F8'
|
|
14
|
-
}, {
|
|
15
|
-
name: 'dark',
|
|
16
|
-
value: '#333333'
|
|
17
|
-
}]
|
|
18
|
-
}
|
|
19
|
-
};
|
package/dist/esm/types/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Background } from '../types';
|
|
2
|
-
export declare const isReduceMotionEnabled: () => any;
|
|
3
|
-
export declare const getBackgroundColorByName: (currentSelectedValue: string, backgrounds: Background[], defaultName: string) => string;
|
|
4
|
-
export declare const clearStyles: (selector: string | string[]) => void;
|
|
5
|
-
export declare const addGridStyle: (selector: string, css: string) => void;
|
|
6
|
-
export declare const addBackgroundStyle: (selector: string, css: string, storyId: string) => void;
|
package/dist/types/index.d.ts
DELETED
package/dist/types/manager.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types/preview.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export declare const decorators: ((StoryFn: import("@storybook/csf").PartialStoryFn<import("@storybook/csf").AnyFramework, import("@storybook/csf").Args>, context: import("@storybook/csf").StoryContext<import("@storybook/csf").AnyFramework, import("@storybook/csf").Args>) => unknown)[];
|
|
2
|
-
export declare const parameters: {
|
|
3
|
-
backgrounds: {
|
|
4
|
-
grid: {
|
|
5
|
-
cellSize: number;
|
|
6
|
-
opacity: number;
|
|
7
|
-
cellAmount: number;
|
|
8
|
-
};
|
|
9
|
-
values: {
|
|
10
|
-
name: string;
|
|
11
|
-
value: string;
|
|
12
|
-
}[];
|
|
13
|
-
};
|
|
14
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ReactElement } from 'react';
|
|
2
|
-
export interface GlobalState {
|
|
3
|
-
name: string | undefined;
|
|
4
|
-
selected: string | undefined;
|
|
5
|
-
}
|
|
6
|
-
export interface BackgroundSelectorItem {
|
|
7
|
-
id: string;
|
|
8
|
-
title: string;
|
|
9
|
-
onClick: () => void;
|
|
10
|
-
value: string;
|
|
11
|
-
active: boolean;
|
|
12
|
-
right?: ReactElement;
|
|
13
|
-
}
|
|
14
|
-
export interface Background {
|
|
15
|
-
name: string;
|
|
16
|
-
value: string;
|
|
17
|
-
}
|
|
18
|
-
export interface BackgroundsParameter {
|
|
19
|
-
default?: string;
|
|
20
|
-
disable?: boolean;
|
|
21
|
-
values: Background[];
|
|
22
|
-
}
|
|
23
|
-
export interface BackgroundsConfig {
|
|
24
|
-
backgrounds: Background[] | null;
|
|
25
|
-
selectedBackgroundName: string | null;
|
|
26
|
-
defaultBackgroundName: string | null;
|
|
27
|
-
disable: boolean;
|
|
28
|
-
}
|