@storybook/addon-backgrounds 6.5.7 → 7.0.0-alpha.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.
Files changed (43) hide show
  1. package/dist/esm/components/ColorIcon.js +13 -15
  2. package/dist/esm/constants.js +5 -5
  3. package/dist/esm/containers/BackgroundSelector.js +39 -82
  4. package/dist/esm/containers/GridSelector.js +12 -44
  5. package/dist/esm/decorators/withBackground.js +20 -21
  6. package/dist/esm/decorators/withGrid.js +40 -33
  7. package/dist/esm/helpers/index.js +27 -43
  8. package/dist/esm/manager.js +5 -10
  9. package/dist/esm/preview.js +2 -2
  10. package/dist/{ts3.9 → types}/components/ColorIcon.d.ts +0 -0
  11. package/dist/{ts3.9 → types}/constants.d.ts +0 -0
  12. package/dist/{ts3.9 → types}/containers/BackgroundSelector.d.ts +0 -0
  13. package/dist/{ts3.9 → types}/containers/GridSelector.d.ts +0 -0
  14. package/dist/{ts3.9 → types}/decorators/withBackground.d.ts +0 -0
  15. package/dist/{ts3.9 → types}/decorators/withGrid.d.ts +0 -0
  16. package/dist/{ts3.9 → types}/helpers/index.d.ts +0 -0
  17. package/dist/{ts3.9 → types}/index.d.ts +0 -0
  18. package/dist/{ts3.9 → types}/manager.d.ts +0 -0
  19. package/dist/{ts3.9 → types}/preview.d.ts +0 -0
  20. package/dist/{ts3.9 → types}/types/index.d.ts +0 -0
  21. package/package.json +9 -20
  22. package/dist/modern/components/ColorIcon.js +0 -14
  23. package/dist/modern/constants.js +0 -6
  24. package/dist/modern/containers/BackgroundSelector.js +0 -93
  25. package/dist/modern/containers/GridSelector.js +0 -34
  26. package/dist/modern/decorators/withBackground.js +0 -42
  27. package/dist/modern/decorators/withGrid.js +0 -67
  28. package/dist/modern/helpers/index.js +0 -86
  29. package/dist/modern/index.js +0 -6
  30. package/dist/modern/manager.js +0 -15
  31. package/dist/modern/preview.js +0 -19
  32. package/dist/modern/types/index.js +0 -1
  33. package/dist/ts3.4/components/ColorIcon.d.ts +0 -3
  34. package/dist/ts3.4/constants.d.ts +0 -6
  35. package/dist/ts3.4/containers/BackgroundSelector.d.ts +0 -2
  36. package/dist/ts3.4/containers/GridSelector.d.ts +0 -2
  37. package/dist/ts3.4/decorators/withBackground.d.ts +0 -2
  38. package/dist/ts3.4/decorators/withGrid.d.ts +0 -2
  39. package/dist/ts3.4/helpers/index.d.ts +0 -6
  40. package/dist/ts3.4/index.d.ts +0 -2
  41. package/dist/ts3.4/manager.d.ts +0 -1
  42. package/dist/ts3.4/preview.d.ts +0 -14
  43. package/dist/ts3.4/types/index.d.ts +0 -28
@@ -1,16 +1,14 @@
1
1
  import { styled } from '@storybook/theming';
2
- export var ColorIcon = styled.span(function (_ref) {
3
- var background = _ref.background;
4
- return {
5
- borderRadius: '1rem',
6
- display: 'block',
7
- height: '1rem',
8
- width: '1rem',
9
- background: background
10
- };
11
- }, function (_ref2) {
12
- var theme = _ref2.theme;
13
- return {
14
- boxShadow: "".concat(theme.appBorderColor, " 0 0 0 1px inset")
15
- };
16
- });
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
+ }));
@@ -1,6 +1,6 @@
1
- export var ADDON_ID = 'storybook/background';
2
- export var PARAM_KEY = 'backgrounds';
3
- export var GRID_PARAM_KEY = 'grid';
4
- export var EVENTS = {
5
- UPDATE: "".concat(ADDON_ID, "/update")
1
+ export const ADDON_ID = 'storybook/background';
2
+ export const PARAM_KEY = 'backgrounds';
3
+ export const GRID_PARAM_KEY = 'grid';
4
+ export const EVENTS = {
5
+ UPDATE: `${ADDON_ID}/update`
6
6
  };
@@ -1,39 +1,3 @@
1
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
-
3
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
4
-
5
- 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."); }
6
-
7
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
8
-
9
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
10
-
11
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
12
-
13
- 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."); }
14
-
15
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
16
-
17
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
18
-
19
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
20
-
21
- 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; }
22
-
23
- import "core-js/modules/es.array.map.js";
24
- import "core-js/modules/es.function.name.js";
25
- import "core-js/modules/es.array.concat.js";
26
- import "core-js/modules/es.array.iterator.js";
27
- import "core-js/modules/es.object.to-string.js";
28
- import "core-js/modules/web.dom-collections.iterator.js";
29
- import "core-js/modules/es.object.assign.js";
30
- import "core-js/modules/es.symbol.js";
31
- import "core-js/modules/es.symbol.description.js";
32
- import "core-js/modules/es.symbol.iterator.js";
33
- import "core-js/modules/es.string.iterator.js";
34
- import "core-js/modules/es.array.from.js";
35
- import "core-js/modules/es.array.slice.js";
36
- import "core-js/modules/es.regexp.exec.js";
37
1
  import React, { Fragment, useCallback, useMemo, memo } from 'react';
38
2
  import memoize from 'memoizerific';
39
3
  import { useParameter, useGlobals } from '@storybook/api';
@@ -42,53 +6,43 @@ import { Icons, IconButton, WithTooltip, TooltipLinkList } from '@storybook/comp
42
6
  import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
43
7
  import { ColorIcon } from '../components/ColorIcon';
44
8
  import { getBackgroundColorByName } from '../helpers';
45
- var createBackgroundSelectorItem = memoize(1000)(function (id, name, value, hasSwatch, change, active) {
46
- return {
47
- id: id || name,
48
- title: name,
49
- onClick: function onClick() {
50
- change({
51
- selected: value,
52
- name: name
53
- });
54
- },
55
- value: value,
56
- right: hasSwatch ? /*#__PURE__*/React.createElement(ColorIcon, {
57
- background: value
58
- }) : undefined,
59
- active: active
60
- };
61
- });
62
- var getDisplayedItems = memoize(10)(function (backgrounds, selectedBackgroundColor, change) {
63
- var backgroundSelectorItems = backgrounds.map(function (_ref) {
64
- var name = _ref.name,
65
- value = _ref.value;
66
- return createBackgroundSelectorItem(null, name, value, true, change, value === selectedBackgroundColor);
67
- });
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));
68
29
 
69
30
  if (selectedBackgroundColor !== 'transparent') {
70
- return [createBackgroundSelectorItem('reset', 'Clear background', 'transparent', null, change, false)].concat(_toConsumableArray(backgroundSelectorItems));
31
+ return [createBackgroundSelectorItem('reset', 'Clear background', 'transparent', null, change, false), ...backgroundSelectorItems];
71
32
  }
72
33
 
73
34
  return backgroundSelectorItems;
74
35
  });
75
- var DEFAULT_BACKGROUNDS_CONFIG = {
36
+ const DEFAULT_BACKGROUNDS_CONFIG = {
76
37
  default: null,
77
38
  disable: true,
78
39
  values: []
79
40
  };
80
- export var BackgroundSelector = /*#__PURE__*/memo(function () {
81
- var _globals$BACKGROUNDS_;
82
-
83
- var backgroundsConfig = useParameter(BACKGROUNDS_PARAM_KEY, DEFAULT_BACKGROUNDS_CONFIG);
84
-
85
- var _useGlobals = useGlobals(),
86
- _useGlobals2 = _slicedToArray(_useGlobals, 2),
87
- globals = _useGlobals2[0],
88
- updateGlobals = _useGlobals2[1];
89
-
90
- var globalsBackgroundColor = (_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.value;
91
- var selectedBackgroundColor = useMemo(function () {
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(() => {
92
46
  return getBackgroundColorByName(globalsBackgroundColor, backgroundsConfig.values, backgroundsConfig.default);
93
47
  }, [backgroundsConfig, globalsBackgroundColor]);
94
48
 
@@ -96,10 +50,12 @@ export var BackgroundSelector = /*#__PURE__*/memo(function () {
96
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');
97
51
  }
98
52
 
99
- var onBackgroundChange = useCallback(function (value) {
100
- updateGlobals(_defineProperty({}, BACKGROUNDS_PARAM_KEY, Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
101
- value: value
102
- })));
53
+ const onBackgroundChange = useCallback(value => {
54
+ updateGlobals({
55
+ [BACKGROUNDS_PARAM_KEY]: Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
56
+ value
57
+ })
58
+ });
103
59
  }, [backgroundsConfig, globals, updateGlobals]);
104
60
 
105
61
  if (backgroundsConfig.disable) {
@@ -110,12 +66,13 @@ export var BackgroundSelector = /*#__PURE__*/memo(function () {
110
66
  placement: "top",
111
67
  trigger: "click",
112
68
  closeOnClick: true,
113
- tooltip: function tooltip(_ref2) {
114
- var onHide = _ref2.onHide;
69
+ tooltip: ({
70
+ onHide
71
+ }) => {
115
72
  return /*#__PURE__*/React.createElement(TooltipLinkList, {
116
- links: getDisplayedItems(backgroundsConfig.values, selectedBackgroundColor, function (_ref3) {
117
- var selected = _ref3.selected;
118
-
73
+ links: getDisplayedItems(backgroundsConfig.values, selectedBackgroundColor, ({
74
+ selected
75
+ }) => {
119
76
  if (selectedBackgroundColor !== selected) {
120
77
  onBackgroundChange(selected);
121
78
  }
@@ -1,63 +1,31 @@
1
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
-
3
- import "core-js/modules/es.object.assign.js";
4
- import "core-js/modules/es.symbol.js";
5
- import "core-js/modules/es.symbol.description.js";
6
- import "core-js/modules/es.object.to-string.js";
7
- import "core-js/modules/es.symbol.iterator.js";
8
- import "core-js/modules/es.array.iterator.js";
9
- import "core-js/modules/es.string.iterator.js";
10
- import "core-js/modules/web.dom-collections.iterator.js";
11
- import "core-js/modules/es.array.slice.js";
12
- import "core-js/modules/es.function.name.js";
13
- import "core-js/modules/es.array.from.js";
14
- import "core-js/modules/es.regexp.exec.js";
15
-
16
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
17
-
18
- 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."); }
19
-
20
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
21
-
22
- 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; }
23
-
24
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
25
-
26
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
27
-
28
1
  import React, { memo } from 'react';
29
2
  import { useGlobals, useParameter } from '@storybook/api';
30
3
  import { Icons, IconButton } from '@storybook/components';
31
4
  import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
32
- export var GridSelector = /*#__PURE__*/memo(function () {
33
- var _globals$BACKGROUNDS_;
34
-
35
- var _useGlobals = useGlobals(),
36
- _useGlobals2 = _slicedToArray(_useGlobals, 2),
37
- globals = _useGlobals2[0],
38
- updateGlobals = _useGlobals2[1];
39
-
40
- var _useParameter = useParameter(BACKGROUNDS_PARAM_KEY, {
5
+ export const GridSelector = /*#__PURE__*/memo(() => {
6
+ const [globals, updateGlobals] = useGlobals();
7
+ const {
8
+ grid
9
+ } = useParameter(BACKGROUNDS_PARAM_KEY, {
41
10
  grid: {
42
11
  disable: false
43
12
  }
44
- }),
45
- grid = _useParameter.grid;
13
+ });
46
14
 
47
- if (grid !== null && grid !== void 0 && grid.disable) {
15
+ if (grid?.disable) {
48
16
  return null;
49
17
  }
50
18
 
51
- var isActive = ((_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.grid) || false;
19
+ const isActive = globals[BACKGROUNDS_PARAM_KEY]?.grid || false;
52
20
  return /*#__PURE__*/React.createElement(IconButton, {
53
21
  key: "background",
54
22
  active: isActive,
55
23
  title: "Apply a grid to the preview",
56
- onClick: function onClick() {
57
- return updateGlobals(_defineProperty({}, BACKGROUNDS_PARAM_KEY, Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
24
+ onClick: () => updateGlobals({
25
+ [BACKGROUNDS_PARAM_KEY]: Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
58
26
  grid: !isActive
59
- })));
60
- }
27
+ })
28
+ })
61
29
  }, /*#__PURE__*/React.createElement(Icons, {
62
30
  icon: "grid"
63
31
  }));
@@ -1,34 +1,33 @@
1
- import "core-js/modules/es.array.iterator.js";
2
- import "core-js/modules/es.object.to-string.js";
3
- import "core-js/modules/web.dom-collections.iterator.js";
4
- import "core-js/modules/es.array.concat.js";
5
1
  import { useMemo, useEffect } from '@storybook/addons';
6
2
  import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
7
3
  import { clearStyles, addBackgroundStyle, getBackgroundColorByName, isReduceMotionEnabled } from '../helpers';
8
- export var withBackground = function withBackground(StoryFn, context) {
9
- var _globals$BACKGROUNDS_;
10
-
11
- var globals = context.globals,
12
- parameters = context.parameters;
13
- var globalsBackgroundColor = (_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.value;
14
- var backgroundsConfig = parameters[BACKGROUNDS_PARAM_KEY];
15
- var selectedBackgroundColor = useMemo(function () {
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(() => {
16
12
  if (backgroundsConfig.disable) {
17
13
  return 'transparent';
18
14
  }
19
15
 
20
16
  return getBackgroundColorByName(globalsBackgroundColor, backgroundsConfig.values, backgroundsConfig.default);
21
17
  }, [backgroundsConfig, globalsBackgroundColor]);
22
- var isActive = useMemo(function () {
23
- return selectedBackgroundColor && selectedBackgroundColor !== 'transparent';
24
- }, [selectedBackgroundColor]);
25
- var selector = context.viewMode === 'docs' ? "#anchor--".concat(context.id, " .docs-story") : '.sb-show-main';
26
- var backgroundStyles = useMemo(function () {
27
- var transitionStyle = 'transition: background-color 0.3s;';
28
- return "\n ".concat(selector, " {\n background: ").concat(selectedBackgroundColor, " !important;\n ").concat(isReduceMotionEnabled() ? '' : transitionStyle, "\n }\n ");
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
+ `;
29
28
  }, [selectedBackgroundColor, selector]);
30
- useEffect(function () {
31
- var selectorId = context.viewMode === 'docs' ? "addon-backgrounds-docs-".concat(context.id) : "addon-backgrounds-color";
29
+ useEffect(() => {
30
+ const selectorId = context.viewMode === 'docs' ? `addon-backgrounds-docs-${context.id}` : `addon-backgrounds-color`;
32
31
 
33
32
  if (!isActive) {
34
33
  clearStyles(selectorId);
@@ -1,51 +1,58 @@
1
- import "core-js/modules/es.array.slice.js";
2
- import "core-js/modules/es.object.freeze.js";
3
-
4
- var _templateObject;
5
-
6
- import "core-js/modules/es.array.join.js";
7
- import "core-js/modules/es.array.concat.js";
8
-
9
- function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
10
-
11
1
  import dedent from 'ts-dedent';
12
2
  import deprecate from 'util-deprecate';
13
3
  import { useMemo, useEffect } from '@storybook/addons';
14
4
  import { clearStyles, addGridStyle } from '../helpers';
15
5
  import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
16
- var deprecatedCellSizeWarning = deprecate(function () {}, dedent(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n Backgrounds Addon: The cell size parameter has been changed.\n\n - parameters.grid.cellSize should now be parameters.backgrounds.grid.cellSize\n See https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-grid-parameter\n "]))));
17
- export var withGrid = function withGrid(StoryFn, context) {
18
- var _globals$BACKGROUNDS_, _parameters$grid, _gridParameters$offse, _gridParameters$offse2;
6
+ const deprecatedCellSizeWarning = deprecate(() => {}, dedent`
7
+ Backgrounds Addon: The cell size parameter has been changed.
19
8
 
20
- var globals = context.globals,
21
- parameters = context.parameters;
22
- var gridParameters = parameters[BACKGROUNDS_PARAM_KEY].grid;
23
- var isActive = ((_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.grid) === true && gridParameters.disable !== true;
24
- var cellAmount = gridParameters.cellAmount,
25
- cellSize = gridParameters.cellSize,
26
- opacity = gridParameters.opacity;
27
- var isInDocs = context.viewMode === 'docs';
28
- var gridSize;
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;
29
26
 
30
- if ((_parameters$grid = parameters.grid) !== null && _parameters$grid !== void 0 && _parameters$grid.cellSize) {
27
+ if (parameters.grid?.cellSize) {
31
28
  gridSize = parameters.grid.cellSize;
32
29
  deprecatedCellSizeWarning();
33
30
  } else {
34
31
  gridSize = cellSize;
35
32
  }
36
33
 
37
- var isLayoutPadded = parameters.layout === undefined || parameters.layout === 'padded'; // 16px offset in the grid to account for padded layout
34
+ const isLayoutPadded = parameters.layout === undefined || parameters.layout === 'padded'; // 16px offset in the grid to account for padded layout
38
35
 
39
- var defaultOffset = isLayoutPadded ? 16 : 0;
40
- var offsetX = (_gridParameters$offse = gridParameters.offsetX) !== null && _gridParameters$offse !== void 0 ? _gridParameters$offse : isInDocs ? 20 : defaultOffset;
41
- var offsetY = (_gridParameters$offse2 = gridParameters.offsetY) !== null && _gridParameters$offse2 !== void 0 ? _gridParameters$offse2 : isInDocs ? 20 : defaultOffset;
42
- var gridStyles = useMemo(function () {
43
- var selector = context.viewMode === 'docs' ? "#anchor--".concat(context.id, " .docs-story") : '.sb-show-main';
44
- var backgroundSize = ["".concat(gridSize * cellAmount, "px ").concat(gridSize * cellAmount, "px"), "".concat(gridSize * cellAmount, "px ").concat(gridSize * cellAmount, "px"), "".concat(gridSize, "px ").concat(gridSize, "px"), "".concat(gridSize, "px ").concat(gridSize, "px")].join(', ');
45
- return "\n ".concat(selector, " {\n background-size: ").concat(backgroundSize, " !important;\n background-position: ").concat(offsetX, "px ").concat(offsetY, "px, ").concat(offsetX, "px ").concat(offsetY, "px, ").concat(offsetX, "px ").concat(offsetY, "px, ").concat(offsetX, "px ").concat(offsetY, "px !important;\n background-blend-mode: difference !important;\n background-image: linear-gradient(rgba(130, 130, 130, ").concat(opacity, ") 1px, transparent 1px),\n linear-gradient(90deg, rgba(130, 130, 130, ").concat(opacity, ") 1px, transparent 1px),\n linear-gradient(rgba(130, 130, 130, ").concat(opacity / 2, ") 1px, transparent 1px),\n linear-gradient(90deg, rgba(130, 130, 130, ").concat(opacity / 2, ") 1px, transparent 1px) !important;\n }\n ");
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
+ `;
46
53
  }, [gridSize]);
47
- useEffect(function () {
48
- var selectorId = context.viewMode === 'docs' ? "addon-backgrounds-grid-docs-".concat(context.id) : "addon-backgrounds-grid";
54
+ useEffect(() => {
55
+ const selectorId = context.viewMode === 'docs' ? `addon-backgrounds-grid-docs-${context.id}` : `addon-backgrounds-grid`;
49
56
 
50
57
  if (!isActive) {
51
58
  clearStyles(selectorId);
@@ -1,97 +1,81 @@
1
- import "core-js/modules/es.array.slice.js";
2
- import "core-js/modules/es.object.freeze.js";
3
-
4
- var _templateObject;
5
-
6
- function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
7
-
8
- import "core-js/modules/es.array.find.js";
9
- import "core-js/modules/es.object.to-string.js";
10
- import "core-js/modules/es.function.name.js";
11
- import "core-js/modules/es.array.join.js";
12
- import "core-js/modules/es.array.map.js";
13
- import "core-js/modules/web.dom-collections.for-each.js";
14
1
  import global from 'global';
15
2
  import dedent from 'ts-dedent';
16
3
  import { logger } from '@storybook/client-logger';
17
- var document = global.document,
18
- window = global.window;
19
- export var isReduceMotionEnabled = function isReduceMotionEnabled() {
20
- var prefersReduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
4
+ const {
5
+ document,
6
+ window
7
+ } = global;
8
+ export const isReduceMotionEnabled = () => {
9
+ const prefersReduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
21
10
  return prefersReduceMotion.matches;
22
11
  };
23
- export var getBackgroundColorByName = function getBackgroundColorByName(currentSelectedValue) {
24
- var backgrounds = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
25
- var defaultName = arguments.length > 2 ? arguments[2] : undefined;
26
-
12
+ export const getBackgroundColorByName = (currentSelectedValue, backgrounds = [], defaultName) => {
27
13
  if (currentSelectedValue === 'transparent') {
28
14
  return 'transparent';
29
15
  }
30
16
 
31
- if (backgrounds.find(function (background) {
32
- return background.value === currentSelectedValue;
33
- })) {
17
+ if (backgrounds.find(background => background.value === currentSelectedValue)) {
34
18
  return currentSelectedValue;
35
19
  }
36
20
 
37
- var defaultBackground = backgrounds.find(function (background) {
38
- return background.name === defaultName;
39
- });
21
+ const defaultBackground = backgrounds.find(background => background.name === defaultName);
40
22
 
41
23
  if (defaultBackground) {
42
24
  return defaultBackground.value;
43
25
  }
44
26
 
45
27
  if (defaultName) {
46
- var availableColors = backgrounds.map(function (background) {
47
- return background.name;
48
- }).join(', ');
49
- logger.warn(dedent(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n Backgrounds Addon: could not find the default color \"", "\".\n These are the available colors for your story based on your configuration:\n ", ".\n "])), defaultName, availableColors));
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
+ `);
50
34
  }
51
35
 
52
36
  return 'transparent';
53
37
  };
54
- export var clearStyles = function clearStyles(selector) {
55
- var selectors = Array.isArray(selector) ? selector : [selector];
38
+ export const clearStyles = selector => {
39
+ const selectors = Array.isArray(selector) ? selector : [selector];
56
40
  selectors.forEach(clearStyle);
57
41
  };
58
42
 
59
- var clearStyle = function clearStyle(selector) {
60
- var element = document.getElementById(selector);
43
+ const clearStyle = selector => {
44
+ const element = document.getElementById(selector);
61
45
 
62
46
  if (element) {
63
47
  element.parentElement.removeChild(element);
64
48
  }
65
49
  };
66
50
 
67
- export var addGridStyle = function addGridStyle(selector, css) {
68
- var existingStyle = document.getElementById(selector);
51
+ export const addGridStyle = (selector, css) => {
52
+ const existingStyle = document.getElementById(selector);
69
53
 
70
54
  if (existingStyle) {
71
55
  if (existingStyle.innerHTML !== css) {
72
56
  existingStyle.innerHTML = css;
73
57
  }
74
58
  } else {
75
- var style = document.createElement('style');
59
+ const style = document.createElement('style');
76
60
  style.setAttribute('id', selector);
77
61
  style.innerHTML = css;
78
62
  document.head.appendChild(style);
79
63
  }
80
64
  };
81
- export var addBackgroundStyle = function addBackgroundStyle(selector, css, storyId) {
82
- var existingStyle = document.getElementById(selector);
65
+ export const addBackgroundStyle = (selector, css, storyId) => {
66
+ const existingStyle = document.getElementById(selector);
83
67
 
84
68
  if (existingStyle) {
85
69
  if (existingStyle.innerHTML !== css) {
86
70
  existingStyle.innerHTML = css;
87
71
  }
88
72
  } else {
89
- var style = document.createElement('style');
73
+ const style = document.createElement('style');
90
74
  style.setAttribute('id', selector);
91
75
  style.innerHTML = css;
92
- var gridStyleSelector = "addon-backgrounds-grid".concat(storyId ? "-docs-".concat(storyId) : ''); // If grids already exist, we want to add the style tag BEFORE it so the background doesn't override grid
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
93
77
 
94
- var existingGridStyle = document.getElementById(gridStyleSelector);
78
+ const existingGridStyle = document.getElementById(gridStyleSelector);
95
79
 
96
80
  if (existingGridStyle) {
97
81
  existingGridStyle.parentElement.insertBefore(style, existingGridStyle);
@@ -1,20 +1,15 @@
1
- import "core-js/modules/es.regexp.exec.js";
2
- import "core-js/modules/es.string.match.js";
3
1
  import React, { Fragment } from 'react';
4
2
  import { addons, types } from '@storybook/addons';
5
3
  import { ADDON_ID } from './constants';
6
4
  import { BackgroundSelector } from './containers/BackgroundSelector';
7
5
  import { GridSelector } from './containers/GridSelector';
8
- addons.register(ADDON_ID, function () {
6
+ addons.register(ADDON_ID, () => {
9
7
  addons.add(ADDON_ID, {
10
8
  title: 'Backgrounds',
11
9
  type: types.TOOL,
12
- match: function match(_ref) {
13
- var viewMode = _ref.viewMode;
14
- return !!(viewMode && viewMode.match(/^(story|docs)$/));
15
- },
16
- render: function render() {
17
- return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(BackgroundSelector, null), /*#__PURE__*/React.createElement(GridSelector, null));
18
- }
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))
19
14
  });
20
15
  });
@@ -1,7 +1,7 @@
1
1
  import { withBackground } from './decorators/withBackground';
2
2
  import { withGrid } from './decorators/withGrid';
3
- export var decorators = [withGrid, withBackground];
4
- export var parameters = {
3
+ export const decorators = [withGrid, withBackground];
4
+ export const parameters = {
5
5
  backgrounds: {
6
6
  grid: {
7
7
  cellSize: 20,
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/addon-backgrounds",
3
- "version": "6.5.7",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "Switch backgrounds to view components in different settings",
5
5
  "keywords": [
6
6
  "addon",
@@ -27,14 +27,7 @@
27
27
  "author": "jbaxleyiii",
28
28
  "main": "dist/cjs/index.js",
29
29
  "module": "dist/esm/index.js",
30
- "types": "dist/ts3.9/index.d.ts",
31
- "typesVersions": {
32
- "<3.8": {
33
- "dist/ts3.9/*": [
34
- "dist/ts3.4/*"
35
- ]
36
- }
37
- },
30
+ "types": "dist/types/index.d.ts",
38
31
  "files": [
39
32
  "dist/**/*",
40
33
  "README.md",
@@ -45,13 +38,13 @@
45
38
  "prepare": "node ../../scripts/prepare.js"
46
39
  },
47
40
  "dependencies": {
48
- "@storybook/addons": "6.5.7",
49
- "@storybook/api": "6.5.7",
50
- "@storybook/client-logger": "6.5.7",
51
- "@storybook/components": "6.5.7",
52
- "@storybook/core-events": "6.5.7",
41
+ "@storybook/addons": "7.0.0-alpha.0",
42
+ "@storybook/api": "7.0.0-alpha.0",
43
+ "@storybook/client-logger": "7.0.0-alpha.0",
44
+ "@storybook/components": "7.0.0-alpha.0",
45
+ "@storybook/core-events": "7.0.0-alpha.0",
53
46
  "@storybook/csf": "0.0.2--canary.4566f4d.1",
54
- "@storybook/theming": "6.5.7",
47
+ "@storybook/theming": "7.0.0-alpha.0",
55
48
  "core-js": "^3.8.2",
56
49
  "global": "^4.4.0",
57
50
  "memoizerific": "^1.11.3",
@@ -59,9 +52,6 @@
59
52
  "ts-dedent": "^2.0.0",
60
53
  "util-deprecate": "^1.0.2"
61
54
  },
62
- "devDependencies": {
63
- "@types/webpack-env": "^1.16.0"
64
- },
65
55
  "peerDependencies": {
66
56
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
67
57
  "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
@@ -77,8 +67,7 @@
77
67
  "publishConfig": {
78
68
  "access": "public"
79
69
  },
80
- "gitHead": "7e19d3e734efa14e7cffba146295ca0d3467a8c6",
81
- "sbmodern": "dist/modern/index.js",
70
+ "gitHead": "c82d897ea765da8cf4fbbcc2af1f28c808a93e23",
82
71
  "storybook": {
83
72
  "displayName": "Backgrounds",
84
73
  "icon": "https://user-images.githubusercontent.com/263385/101991667-479cc600-3c7c-11eb-96d3-410e936252e7.png",
@@ -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
- }));
@@ -1,6 +0,0 @@
1
- export const ADDON_ID = 'storybook/background';
2
- export const PARAM_KEY = 'backgrounds';
3
- export const GRID_PARAM_KEY = 'grid';
4
- export const EVENTS = {
5
- UPDATE: `${ADDON_ID}/update`
6
- };
@@ -1,93 +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
- var _globals$BACKGROUNDS_;
43
-
44
- const backgroundsConfig = useParameter(BACKGROUNDS_PARAM_KEY, DEFAULT_BACKGROUNDS_CONFIG);
45
- const [globals, updateGlobals] = useGlobals();
46
- const globalsBackgroundColor = (_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.value;
47
- const selectedBackgroundColor = useMemo(() => {
48
- return getBackgroundColorByName(globalsBackgroundColor, backgroundsConfig.values, backgroundsConfig.default);
49
- }, [backgroundsConfig, globalsBackgroundColor]);
50
-
51
- if (Array.isArray(backgroundsConfig)) {
52
- 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');
53
- }
54
-
55
- const onBackgroundChange = useCallback(value => {
56
- updateGlobals({
57
- [BACKGROUNDS_PARAM_KEY]: Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
58
- value
59
- })
60
- });
61
- }, [backgroundsConfig, globals, updateGlobals]);
62
-
63
- if (backgroundsConfig.disable) {
64
- return null;
65
- }
66
-
67
- return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(WithTooltip, {
68
- placement: "top",
69
- trigger: "click",
70
- closeOnClick: true,
71
- tooltip: ({
72
- onHide
73
- }) => {
74
- return /*#__PURE__*/React.createElement(TooltipLinkList, {
75
- links: getDisplayedItems(backgroundsConfig.values, selectedBackgroundColor, ({
76
- selected
77
- }) => {
78
- if (selectedBackgroundColor !== selected) {
79
- onBackgroundChange(selected);
80
- }
81
-
82
- onHide();
83
- })
84
- });
85
- }
86
- }, /*#__PURE__*/React.createElement(IconButton, {
87
- key: "background",
88
- title: "Change the background of the preview",
89
- active: selectedBackgroundColor !== 'transparent'
90
- }, /*#__PURE__*/React.createElement(Icons, {
91
- icon: "photo"
92
- }))));
93
- });
@@ -1,34 +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
- var _globals$BACKGROUNDS_;
7
-
8
- const [globals, updateGlobals] = useGlobals();
9
- const {
10
- grid
11
- } = useParameter(BACKGROUNDS_PARAM_KEY, {
12
- grid: {
13
- disable: false
14
- }
15
- });
16
-
17
- if (grid !== null && grid !== void 0 && grid.disable) {
18
- return null;
19
- }
20
-
21
- const isActive = ((_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.grid) || false;
22
- return /*#__PURE__*/React.createElement(IconButton, {
23
- key: "background",
24
- active: isActive,
25
- title: "Apply a grid to the preview",
26
- onClick: () => updateGlobals({
27
- [BACKGROUNDS_PARAM_KEY]: Object.assign({}, globals[BACKGROUNDS_PARAM_KEY], {
28
- grid: !isActive
29
- })
30
- })
31
- }, /*#__PURE__*/React.createElement(Icons, {
32
- icon: "grid"
33
- }));
34
- });
@@ -1,42 +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
- var _globals$BACKGROUNDS_;
6
-
7
- const {
8
- globals,
9
- parameters
10
- } = context;
11
- const globalsBackgroundColor = (_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.value;
12
- const backgroundsConfig = parameters[BACKGROUNDS_PARAM_KEY];
13
- const selectedBackgroundColor = useMemo(() => {
14
- if (backgroundsConfig.disable) {
15
- return 'transparent';
16
- }
17
-
18
- return getBackgroundColorByName(globalsBackgroundColor, backgroundsConfig.values, backgroundsConfig.default);
19
- }, [backgroundsConfig, globalsBackgroundColor]);
20
- const isActive = useMemo(() => selectedBackgroundColor && selectedBackgroundColor !== 'transparent', [selectedBackgroundColor]);
21
- const selector = context.viewMode === 'docs' ? `#anchor--${context.id} .docs-story` : '.sb-show-main';
22
- const backgroundStyles = useMemo(() => {
23
- const transitionStyle = 'transition: background-color 0.3s;';
24
- return `
25
- ${selector} {
26
- background: ${selectedBackgroundColor} !important;
27
- ${isReduceMotionEnabled() ? '' : transitionStyle}
28
- }
29
- `;
30
- }, [selectedBackgroundColor, selector]);
31
- useEffect(() => {
32
- const selectorId = context.viewMode === 'docs' ? `addon-backgrounds-docs-${context.id}` : `addon-backgrounds-color`;
33
-
34
- if (!isActive) {
35
- clearStyles(selectorId);
36
- return;
37
- }
38
-
39
- addBackgroundStyle(selectorId, backgroundStyles, context.viewMode === 'docs' ? context.id : null);
40
- }, [isActive, backgroundStyles, context]);
41
- return StoryFn();
42
- };
@@ -1,67 +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
- var _globals$BACKGROUNDS_, _parameters$grid, _gridParameters$offse, _gridParameters$offse2;
14
-
15
- const {
16
- globals,
17
- parameters
18
- } = context;
19
- const gridParameters = parameters[BACKGROUNDS_PARAM_KEY].grid;
20
- const isActive = ((_globals$BACKGROUNDS_ = globals[BACKGROUNDS_PARAM_KEY]) === null || _globals$BACKGROUNDS_ === void 0 ? void 0 : _globals$BACKGROUNDS_.grid) === true && gridParameters.disable !== true;
21
- const {
22
- cellAmount,
23
- cellSize,
24
- opacity
25
- } = gridParameters;
26
- const isInDocs = context.viewMode === 'docs';
27
- let gridSize;
28
-
29
- if ((_parameters$grid = parameters.grid) !== null && _parameters$grid !== void 0 && _parameters$grid.cellSize) {
30
- gridSize = parameters.grid.cellSize;
31
- deprecatedCellSizeWarning();
32
- } else {
33
- gridSize = cellSize;
34
- }
35
-
36
- const isLayoutPadded = parameters.layout === undefined || parameters.layout === 'padded'; // 16px offset in the grid to account for padded layout
37
-
38
- const defaultOffset = isLayoutPadded ? 16 : 0;
39
- const offsetX = (_gridParameters$offse = gridParameters.offsetX) !== null && _gridParameters$offse !== void 0 ? _gridParameters$offse : isInDocs ? 20 : defaultOffset;
40
- const offsetY = (_gridParameters$offse2 = gridParameters.offsetY) !== null && _gridParameters$offse2 !== void 0 ? _gridParameters$offse2 : isInDocs ? 20 : defaultOffset;
41
- const gridStyles = useMemo(() => {
42
- const selector = context.viewMode === 'docs' ? `#anchor--${context.id} .docs-story` : '.sb-show-main';
43
- const backgroundSize = [`${gridSize * cellAmount}px ${gridSize * cellAmount}px`, `${gridSize * cellAmount}px ${gridSize * cellAmount}px`, `${gridSize}px ${gridSize}px`, `${gridSize}px ${gridSize}px`].join(', ');
44
- return `
45
- ${selector} {
46
- background-size: ${backgroundSize} !important;
47
- background-position: ${offsetX}px ${offsetY}px, ${offsetX}px ${offsetY}px, ${offsetX}px ${offsetY}px, ${offsetX}px ${offsetY}px !important;
48
- background-blend-mode: difference !important;
49
- background-image: linear-gradient(rgba(130, 130, 130, ${opacity}) 1px, transparent 1px),
50
- linear-gradient(90deg, rgba(130, 130, 130, ${opacity}) 1px, transparent 1px),
51
- linear-gradient(rgba(130, 130, 130, ${opacity / 2}) 1px, transparent 1px),
52
- linear-gradient(90deg, rgba(130, 130, 130, ${opacity / 2}) 1px, transparent 1px) !important;
53
- }
54
- `;
55
- }, [gridSize]);
56
- useEffect(() => {
57
- const selectorId = context.viewMode === 'docs' ? `addon-backgrounds-grid-docs-${context.id}` : `addon-backgrounds-grid`;
58
-
59
- if (!isActive) {
60
- clearStyles(selectorId);
61
- return;
62
- }
63
-
64
- addGridStyle(selectorId, gridStyles);
65
- }, [isActive, gridStyles, context]);
66
- return StoryFn();
67
- };
@@ -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
- };
@@ -1,6 +0,0 @@
1
- if (module && module.hot && module.hot.decline) {
2
- module.hot.decline();
3
- } // make it work with --isolatedModules
4
-
5
-
6
- export default {};
@@ -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
- });
@@ -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
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- export declare const ColorIcon: import("@storybook/theming").StyledComponent<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, {
2
- background: string;
3
- }, import("@storybook/theming").Theme>;
@@ -1,6 +0,0 @@
1
- export declare const ADDON_ID = "storybook/background";
2
- export declare const PARAM_KEY = "backgrounds";
3
- export declare const GRID_PARAM_KEY = "grid";
4
- export declare const EVENTS: {
5
- UPDATE: string;
6
- };
@@ -1,2 +0,0 @@
1
- import { FunctionComponent } from 'react';
2
- export declare const BackgroundSelector: FunctionComponent;
@@ -1,2 +0,0 @@
1
- import { FunctionComponent } from 'react';
2
- export declare const GridSelector: FunctionComponent;
@@ -1,2 +0,0 @@
1
- import { AnyFramework, PartialStoryFn as StoryFunction, StoryContext } from '@storybook/csf';
2
- export declare const withBackground: (StoryFn: StoryFunction<AnyFramework>, context: StoryContext<AnyFramework>) => unknown;
@@ -1,2 +0,0 @@
1
- import { AnyFramework, PartialStoryFn as StoryFunction, StoryContext } from '@storybook/csf';
2
- export declare const withGrid: (StoryFn: StoryFunction<AnyFramework>, context: StoryContext<AnyFramework>) => unknown;
@@ -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;
@@ -1,2 +0,0 @@
1
- declare const _default: {};
2
- export default _default;
@@ -1 +0,0 @@
1
- export {};
@@ -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
- }