@itcase/storybook-config 1.3.23 → 1.3.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ interface A4ColorModeWarningProps {
2
+ message: string;
3
+ pinned?: boolean;
4
+ }
5
+ declare function A4ColorModeWarning(props: A4ColorModeWarningProps): import("react/jsx-runtime").JSX.Element;
6
+ export { A4ColorModeWarning };
7
+ export type { A4ColorModeWarningProps };
@@ -0,0 +1,44 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createPortal } from 'react-dom';
3
+ const warningFontFamily = 'system-ui, sans-serif';
4
+ const warningStyle = {
5
+ width: '100%',
6
+ border: '1px solid #f5c2c7',
7
+ borderRadius: 4,
8
+ marginBottom: 12,
9
+ padding: '8px 12px',
10
+ color: '#842029',
11
+ fontFamily: warningFontFamily,
12
+ fontSize: 13,
13
+ lineHeight: '18px',
14
+ background: '#f8d7da',
15
+ boxSizing: 'border-box',
16
+ };
17
+ const pinnedWarningStyle = {
18
+ width: '100%',
19
+ position: 'fixed',
20
+ top: 0,
21
+ right: 0,
22
+ left: 0,
23
+ zIndex: 2147483000,
24
+ border: 'none',
25
+ borderBottom: '1px solid #f5c2c7',
26
+ borderRadius: 0,
27
+ margin: 0,
28
+ padding: '8px 12px',
29
+ color: '#842029',
30
+ fontFamily: warningFontFamily,
31
+ fontSize: 13,
32
+ lineHeight: '18px',
33
+ background: '#f8d7da',
34
+ boxSizing: 'border-box',
35
+ };
36
+ function A4ColorModeWarning(props) {
37
+ const { message, pinned = false } = props;
38
+ const warning = (_jsx("div", { role: "status", style: pinned ? pinnedWarningStyle : warningStyle, children: message }));
39
+ if (!pinned || typeof document === 'undefined') {
40
+ return warning;
41
+ }
42
+ return createPortal(warning, document.body);
43
+ }
44
+ export { A4ColorModeWarning };
@@ -1,6 +1,12 @@
1
+ declare const A4_COLOR_MODE_COLOR = "color";
2
+ declare const A4_COLOR_MODE_MISSING_BOTH = "\u0414\u043B\u044F viewport A4 \u043D\u0443\u0436\u043D\u043E \u0441\u043E\u0437\u0434\u0430\u0442\u044C \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u044B Color \u0438 Monochrome.";
3
+ declare const A4_COLOR_MODE_MISSING_COLOR = "\u0414\u043B\u044F viewport A4 \u0435\u0441\u0442\u044C \u0432\u0430\u0440\u0438\u0430\u043D\u0442 Monochrome. \u0414\u043E\u0431\u0430\u0432\u044C\u0442\u0435 \u0432\u0430\u0440\u0438\u0430\u043D\u0442 Color.";
4
+ declare const A4_COLOR_MODE_MISSING_MONOCHROME = "\u0414\u043B\u044F viewport A4 \u0435\u0441\u0442\u044C \u0432\u0430\u0440\u0438\u0430\u043D\u0442 Color. \u0414\u043E\u0431\u0430\u0432\u044C\u0442\u0435 \u0432\u0430\u0440\u0438\u0430\u043D\u0442 Monochrome.";
5
+ declare const A4_COLOR_MODE_MONOCHROME = "monochrome";
6
+ declare const A4_COLOR_MODE_PROPERTY_NAME = "Color";
1
7
  declare const VARIANTS_ADDON_ID = "variants";
2
8
  declare const VARIANTS_GLOBAL_KEY = "variants";
3
9
  declare const VARIANTS_PANEL_ID = "variants/panel";
4
10
  declare const VARIANTS_PARAMETER_KEY = "variants";
5
11
  declare const VARIANTS_TOOL_ID = "variants/tool";
6
- export { VARIANTS_ADDON_ID, VARIANTS_GLOBAL_KEY, VARIANTS_PANEL_ID, VARIANTS_PARAMETER_KEY, VARIANTS_TOOL_ID, };
12
+ export { A4_COLOR_MODE_COLOR, A4_COLOR_MODE_MISSING_BOTH, A4_COLOR_MODE_MISSING_COLOR, A4_COLOR_MODE_MISSING_MONOCHROME, A4_COLOR_MODE_MONOCHROME, A4_COLOR_MODE_PROPERTY_NAME, VARIANTS_ADDON_ID, VARIANTS_GLOBAL_KEY, VARIANTS_PANEL_ID, VARIANTS_PARAMETER_KEY, VARIANTS_TOOL_ID, };
@@ -1,6 +1,12 @@
1
+ const A4_COLOR_MODE_COLOR = 'color';
2
+ const A4_COLOR_MODE_MISSING_BOTH = 'Для viewport A4 нужно создать варианты Color и Monochrome.';
3
+ const A4_COLOR_MODE_MISSING_COLOR = 'Для viewport A4 есть вариант Monochrome. Добавьте вариант Color.';
4
+ const A4_COLOR_MODE_MISSING_MONOCHROME = 'Для viewport A4 есть вариант Color. Добавьте вариант Monochrome.';
5
+ const A4_COLOR_MODE_MONOCHROME = 'monochrome';
6
+ const A4_COLOR_MODE_PROPERTY_NAME = 'Color';
1
7
  const VARIANTS_ADDON_ID = 'variants';
2
8
  const VARIANTS_GLOBAL_KEY = 'variants';
3
9
  const VARIANTS_PANEL_ID = 'variants/panel';
4
10
  const VARIANTS_PARAMETER_KEY = 'variants';
5
11
  const VARIANTS_TOOL_ID = 'variants/tool';
6
- export { VARIANTS_ADDON_ID, VARIANTS_GLOBAL_KEY, VARIANTS_PANEL_ID, VARIANTS_PARAMETER_KEY, VARIANTS_TOOL_ID, };
12
+ export { A4_COLOR_MODE_COLOR, A4_COLOR_MODE_MISSING_BOTH, A4_COLOR_MODE_MISSING_COLOR, A4_COLOR_MODE_MISSING_MONOCHROME, A4_COLOR_MODE_MONOCHROME, A4_COLOR_MODE_PROPERTY_NAME, VARIANTS_ADDON_ID, VARIANTS_GLOBAL_KEY, VARIANTS_PANEL_ID, VARIANTS_PARAMETER_KEY, VARIANTS_TOOL_ID, };
@@ -2,8 +2,9 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { ControlsIcon } from '@storybook/icons';
3
3
  import { AddonPanel, Badge, Button, Placeholder, WithTooltip, } from 'storybook/internal/components';
4
4
  import { addons, types, useArgs, useGlobals, useParameter, useStorybookApi, useStorybookState, } from 'storybook/manager-api';
5
+ import { A4ColorModeWarning } from './A4ColorModeWarning.js';
5
6
  import { VARIANTS_ADDON_ID, VARIANTS_GLOBAL_KEY, VARIANTS_PANEL_ID, VARIANTS_PARAMETER_KEY, VARIANTS_TOOL_ID, } from './constants.js';
6
- import { findVariant, getSelectedProperties, isStoryVariantList, replacePropertyValue, } from './variants.helpers.js';
7
+ import { findVariant, getA4ColorModeWarning, getSelectedProperties, isStoryVariantList, replacePropertyValue, } from './variants.helpers.js';
7
8
  import { VariantsSwitchList } from './VariantsCanvas.js';
8
9
  import { VariantsPropertyList } from './VariantsControl.js';
9
10
  function useVariantsState() {
@@ -12,6 +13,11 @@ function useVariantsState() {
12
13
  const [globals, updateGlobals] = useGlobals();
13
14
  const state = useStorybookState();
14
15
  const storyId = state.storyId;
16
+ const colorModeWarning = getA4ColorModeWarning({
17
+ args: args,
18
+ variants: variants,
19
+ viewport: globals.viewport,
20
+ });
15
21
  const hasVariants = isStoryVariantList(variants) && variants.length > 0 && Boolean(storyId);
16
22
  const selectedProperties = hasVariants
17
23
  ? getSelectedProperties({
@@ -36,6 +42,7 @@ function useVariantsState() {
36
42
  });
37
43
  }
38
44
  return {
45
+ colorModeWarning: colorModeWarning,
39
46
  hasVariants: hasVariants,
40
47
  selectedProperties: selectedProperties,
41
48
  selectedVariant: selectedVariant,
@@ -73,11 +80,11 @@ const tooltipStyle = {
73
80
  padding: '4px 8px',
74
81
  };
75
82
  function VariantsPanel() {
76
- const { hasVariants } = useVariantsState();
77
- if (!hasVariants) {
83
+ const { colorModeWarning, hasVariants } = useVariantsState();
84
+ if (!hasVariants && !colorModeWarning) {
78
85
  return _jsx(Placeholder, { children: "No variants for this story" });
79
86
  }
80
- return (_jsx("div", { style: panelStyle, children: _jsx("div", { style: panelInnerStyle, children: _jsx(VariantsControls, {}) }) }));
87
+ return (_jsx("div", { style: panelStyle, children: _jsxs("div", { style: panelInnerStyle, children: [colorModeWarning && _jsx(A4ColorModeWarning, { message: colorModeWarning }), hasVariants ? (_jsx(VariantsControls, {})) : (_jsx(Placeholder, { children: "No variants for this story" }))] }) }));
81
88
  }
82
89
  function VariantsPanelTitle() {
83
90
  const api = useStorybookApi();
@@ -87,15 +94,15 @@ function VariantsPanelTitle() {
87
94
  return (_jsxs("div", { style: titleStyle, children: [_jsx("span", { children: "Variants" }), variantsCount > 0 && (_jsx(Badge, { status: isSelected ? 'active' : 'neutral', compact: true, children: variantsCount }))] }));
88
95
  }
89
96
  function VariantsTool() {
90
- const { hasVariants, selectedProperties, selectedVariant, selectProperties, variants, } = useVariantsState();
91
- if (!hasVariants || !selectedVariant) {
97
+ const { colorModeWarning, hasVariants, selectedProperties, selectedVariant, selectProperties, variants, } = useVariantsState();
98
+ if (!colorModeWarning && (!hasVariants || !selectedVariant)) {
92
99
  return null;
93
100
  }
94
- return (_jsx(WithTooltip, { placement: "bottom-end", tooltip: _jsx("div", { style: tooltipStyle, children: _jsx(VariantsSwitchList, { selectedProperties: selectedProperties, variants: variants, onChange: (propertyName, nextValue) => {
95
- const nextProperties = replacePropertyValue(selectedVariant.properties, propertyName, nextValue);
96
- const nextVariant = findVariant(variants, nextProperties);
97
- selectProperties(nextVariant.properties);
98
- } }) }), trigger: "click", closeOnOutsideClick: true, children: _jsxs(Button, { size: "small", padding: "small", "aria-label": "Variants", variant: "ghost", children: [_jsx(ControlsIcon, {}), "Variants", _jsx(Badge, { status: "neutral", compact: true, children: variants.length })] }) }));
101
+ return (_jsx(WithTooltip, { placement: "bottom-end", tooltip: _jsxs("div", { style: tooltipStyle, children: [colorModeWarning && (_jsx(A4ColorModeWarning, { message: colorModeWarning })), selectedVariant && (_jsx(VariantsSwitchList, { selectedProperties: selectedProperties, variants: variants, onChange: (propertyName, nextValue) => {
102
+ const nextProperties = replacePropertyValue(selectedVariant.properties, propertyName, nextValue);
103
+ const nextVariant = findVariant(variants, nextProperties);
104
+ selectProperties(nextVariant.properties);
105
+ } }))] }), trigger: "click", closeOnOutsideClick: true, children: _jsxs(Button, { size: "small", padding: "small", "aria-label": "Variants", variant: "ghost", children: [_jsx(ControlsIcon, {}), "Variants", _jsx(Badge, { status: "neutral", compact: true, children: variants.length })] }) }));
99
106
  }
100
107
  function VariantsPanelContainer(props) {
101
108
  const { active = false } = props;
@@ -1,17 +1,25 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { withA4PaperDecorator } from '../../decorators/withA4PaperDecorator.js';
3
+ import { A4ColorModeWarning } from './A4ColorModeWarning.js';
3
4
  import { VARIANTS_GLOBAL_KEY, VARIANTS_PARAMETER_KEY } from './constants.js';
4
- import { findVariant, getSelectedProperties, isStoryVariantList, } from './variants.helpers.js';
5
+ import { findVariant, getA4ColorModeWarning, getSelectedProperties, isStoryVariantList, } from './variants.helpers.js';
5
6
  const storyStyle = {
6
7
  width: '100%',
7
8
  };
8
9
  function withVariants(Story, context) {
9
10
  const variants = context.parameters[VARIANTS_PARAMETER_KEY];
10
- if (!isStoryVariantList(variants) || variants.length === 0) {
11
- return _jsx(Story, {});
12
- }
13
- if (context.viewMode === 'docs') {
14
- return _jsx(Story, {});
11
+ const colorModeWarning = getA4ColorModeWarning({
12
+ args: context.args,
13
+ variants: variants,
14
+ viewport: context.globals.viewport,
15
+ });
16
+ if (!isStoryVariantList(variants) ||
17
+ variants.length === 0 ||
18
+ context.viewMode === 'docs') {
19
+ if (!colorModeWarning) {
20
+ return _jsx(Story, {});
21
+ }
22
+ return (_jsxs("div", { style: storyStyle, children: [_jsx(A4ColorModeWarning, { message: colorModeWarning, pinned: true }), _jsx(Story, {})] }));
15
23
  }
16
24
  const selectedProperties = getSelectedProperties({
17
25
  args: context.args,
@@ -27,7 +35,11 @@ function withVariants(Story, context) {
27
35
  ...context,
28
36
  args: selectedVariant.args,
29
37
  });
30
- return withA4PaperDecorator(() => (_jsx("div", { style: storyStyle, children: story }, selectionKey)), context);
38
+ const storyNode = withA4PaperDecorator(() => (_jsx("div", { style: storyStyle, children: story }, selectionKey)), context);
39
+ if (!colorModeWarning) {
40
+ return storyNode;
41
+ }
42
+ return (_jsxs("div", { style: storyStyle, children: [_jsx(A4ColorModeWarning, { message: colorModeWarning, pinned: true }), storyNode] }));
31
43
  }
32
44
  const decorators = [withVariants];
33
45
  const globalTypes = {
@@ -1,4 +1,4 @@
1
- import type { GetInitialPropertiesParams, GetSelectedPropertiesParams, StoryVariant, StoryVariantControl, StoryVariantProperty, VariantsGlobal } from './variants.interface.js';
1
+ import type { GetA4ColorModeWarningParams, GetInitialPropertiesParams, GetSelectedPropertiesParams, StoryVariant, StoryVariantControl, StoryVariantProperty, VariantsGlobal } from './variants.interface.js';
2
2
  declare function isStoryVariantList(value: unknown): value is StoryVariant[];
3
3
  declare function isVariantsGlobal(value: unknown): value is VariantsGlobal;
4
4
  declare function getShowCanvas(globalValue: unknown): boolean;
@@ -9,5 +9,6 @@ declare function getPropertyOptions(variants: StoryVariant[], propertyName: stri
9
9
  declare function findVariant(variants: StoryVariant[], properties: StoryVariantProperty[]): StoryVariant;
10
10
  declare function getInitialProperties(params: GetInitialPropertiesParams): StoryVariantProperty[];
11
11
  declare function replacePropertyValue(properties: StoryVariantProperty[], propertyName: string, propertyValue: string): StoryVariantProperty[];
12
+ declare function getA4ColorModeWarning(params: GetA4ColorModeWarningParams): "Для viewport A4 нужно создать варианты Color и Monochrome." | "Для viewport A4 есть вариант Monochrome. Добавьте вариант Color." | "Для viewport A4 есть вариант Color. Добавьте вариант Monochrome.";
12
13
  declare function getSelectedProperties(params: GetSelectedPropertiesParams): StoryVariantProperty[];
13
- export { findVariant, getInitialProperties, getPropertyControl, getPropertyNames, getPropertyOptions, getPropertyValue, getSelectedProperties, getShowCanvas, isStoryVariantList, isVariantsGlobal, replacePropertyValue, };
14
+ export { findVariant, getA4ColorModeWarning, getInitialProperties, getPropertyControl, getPropertyNames, getPropertyOptions, getPropertyValue, getSelectedProperties, getShowCanvas, isStoryVariantList, isVariantsGlobal, replacePropertyValue, };
@@ -1,3 +1,5 @@
1
+ import { isA4Viewport } from '../../decorators/withA4PaperDecorator.js';
2
+ import { A4_COLOR_MODE_COLOR, A4_COLOR_MODE_MISSING_BOTH, A4_COLOR_MODE_MISSING_COLOR, A4_COLOR_MODE_MISSING_MONOCHROME, A4_COLOR_MODE_MONOCHROME, A4_COLOR_MODE_PROPERTY_NAME, } from './constants.js';
1
3
  function isStoryVariantArgs(value) {
2
4
  return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
3
5
  }
@@ -129,6 +131,53 @@ function propertiesBelongToVariants(variants, properties) {
129
131
  const propertyNames = getPropertyNames(variants);
130
132
  return properties.every((property) => propertyNames.includes(property.name));
131
133
  }
134
+ function addColorModeValue(values, value) {
135
+ if (value === A4_COLOR_MODE_COLOR || value === A4_COLOR_MODE_MONOCHROME) {
136
+ values.add(value);
137
+ }
138
+ }
139
+ function readColorModeArgs(values, args) {
140
+ if (!args || typeof args !== 'object' || !('colorMode' in args)) {
141
+ return;
142
+ }
143
+ addColorModeValue(values, args.colorMode);
144
+ }
145
+ function getColorModeValues(variants, args) {
146
+ const values = new Set();
147
+ readColorModeArgs(values, args);
148
+ if (!isStoryVariantList(variants)) {
149
+ return values;
150
+ }
151
+ variants.forEach((variant) => {
152
+ readColorModeArgs(values, variant.args);
153
+ variant.properties.forEach((property) => {
154
+ if (property.name !== A4_COLOR_MODE_PROPERTY_NAME) {
155
+ return;
156
+ }
157
+ addColorModeValue(values, property.value);
158
+ });
159
+ });
160
+ return values;
161
+ }
162
+ function getA4ColorModeWarning(params) {
163
+ const { args, variants, viewport } = params;
164
+ if (!isA4Viewport(viewport)) {
165
+ return undefined;
166
+ }
167
+ const values = getColorModeValues(variants, args);
168
+ const hasColor = values.has(A4_COLOR_MODE_COLOR);
169
+ const hasMonochrome = values.has(A4_COLOR_MODE_MONOCHROME);
170
+ if (hasColor && hasMonochrome) {
171
+ return undefined;
172
+ }
173
+ if (hasColor) {
174
+ return A4_COLOR_MODE_MISSING_MONOCHROME;
175
+ }
176
+ if (hasMonochrome) {
177
+ return A4_COLOR_MODE_MISSING_COLOR;
178
+ }
179
+ return A4_COLOR_MODE_MISSING_BOTH;
180
+ }
132
181
  function getSelectedProperties(params) {
133
182
  const { args, globalValue, storyId, variants } = params;
134
183
  if (isVariantsGlobal(globalValue) &&
@@ -142,4 +191,4 @@ function getSelectedProperties(params) {
142
191
  variants: variants,
143
192
  });
144
193
  }
145
- export { findVariant, getInitialProperties, getPropertyControl, getPropertyNames, getPropertyOptions, getPropertyValue, getSelectedProperties, getShowCanvas, isStoryVariantList, isVariantsGlobal, replacePropertyValue, };
194
+ export { findVariant, getA4ColorModeWarning, getInitialProperties, getPropertyControl, getPropertyNames, getPropertyOptions, getPropertyValue, getSelectedProperties, getShowCanvas, isStoryVariantList, isVariantsGlobal, replacePropertyValue, };
@@ -18,6 +18,11 @@ interface GetInitialPropertiesParams {
18
18
  args: StoryVariantArgs;
19
19
  variants: StoryVariant[];
20
20
  }
21
+ interface GetA4ColorModeWarningParams {
22
+ args: unknown;
23
+ variants: unknown;
24
+ viewport: unknown;
25
+ }
21
26
  interface GetSelectedPropertiesParams {
22
27
  args: StoryVariantArgs;
23
28
  globalValue: unknown;
@@ -39,4 +44,4 @@ interface VariantsPropertyListProps {
39
44
  selectedProperties: StoryVariantProperty[];
40
45
  variants: StoryVariant[];
41
46
  }
42
- export type { GetInitialPropertiesParams, GetSelectedPropertiesParams, StoryVariant, StoryVariantArgs, StoryVariantControl, StoryVariantProperty, VariantsControlProps, VariantsGlobal, VariantsPanelContainerProps, VariantsPropertyListProps, };
47
+ export type { GetA4ColorModeWarningParams, GetInitialPropertiesParams, GetSelectedPropertiesParams, StoryVariant, StoryVariantArgs, StoryVariantControl, StoryVariantProperty, VariantsControlProps, VariantsGlobal, VariantsPanelContainerProps, VariantsPropertyListProps, };
@@ -49,4 +49,5 @@ function withA4PaperDecorator(Story, context) {
49
49
  });
50
50
  }
51
51
 
52
+ exports.isA4Viewport = isA4Viewport;
52
53
  exports.withA4PaperDecorator = withA4PaperDecorator;
@@ -1,3 +1,4 @@
1
1
  import type { Decorator } from '@storybook/react';
2
+ declare function isA4Viewport(viewport: unknown): boolean;
2
3
  declare function withA4PaperDecorator(Story: Parameters<Decorator>[0], context: Parameters<Decorator>[1]): import("react/jsx-runtime").JSX.Element;
3
- export { withA4PaperDecorator };
4
+ export { isA4Viewport, withA4PaperDecorator };
@@ -47,4 +47,4 @@ function withA4PaperDecorator(Story, context) {
47
47
  });
48
48
  }
49
49
 
50
- export { withA4PaperDecorator };
50
+ export { isA4Viewport, withA4PaperDecorator };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itcase/storybook-config",
3
- "version": "1.3.23",
3
+ "version": "1.3.24",
4
4
  "author": "ITCase",
5
5
  "description": "Storybook configuration package",
6
6
  "engines": {