|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import React from 'react'; |
9 | | -import styled from 'styled-components'; |
10 | | -import { Story } from '@storybook/react'; |
11 | | -import { DEFAULT_THEME, getColorV8 } from '@zendeskgarden/react-theming'; |
| 9 | +import { StoryFn } from '@storybook/react'; |
| 10 | +import styled, { DefaultTheme, useTheme } from 'styled-components'; |
| 11 | +import { IGardenTheme, getColor } from '@zendeskgarden/react-theming'; |
| 12 | +import { Tag } from '@zendeskgarden/react-tags'; |
12 | 13 |
|
13 | | -interface IArgs { |
14 | | - hue: string; |
15 | | - shade: number; |
16 | | - transparency?: number; |
17 | | -} |
| 14 | +const toBackground = (theme: DefaultTheme, backgroundColor: string) => { |
| 15 | + const color = getColor({ hue: 'neutralHue', shade: 300, theme }); |
| 16 | + const size = 26; |
| 17 | + const dimensions = `${size}px ${size}px`; |
| 18 | + const positionX1 = theme.rtl ? '100%' : '0'; |
| 19 | + const positionX2 = theme.rtl ? `calc(100% - ${size / 2}px)` : `${size / 2}px`; |
| 20 | + const position1 = `${positionX1} 0`; |
| 21 | + const position2 = `${positionX2} ${size / 2}px`; |
| 22 | + const position3 = `${positionX2} 0`; |
| 23 | + const position4 = `${positionX1} ${size / -2}px`; |
18 | 24 |
|
19 | | -const StyledDiv = styled.div<IArgs>` |
20 | | - background-color: ${props => |
21 | | - getColorV8( |
22 | | - props.hue, |
23 | | - props.shade, |
24 | | - DEFAULT_THEME, |
25 | | - props.transparency ? props.transparency / 100 : undefined |
26 | | - )}; |
| 25 | + return ` |
| 26 | + linear-gradient(${backgroundColor}, ${backgroundColor}), |
| 27 | + linear-gradient(45deg, ${color} 25%, transparent 25%) ${position1} / ${dimensions} repeat, |
| 28 | + linear-gradient(45deg, transparent 75%, ${color} 75%) ${position2} / ${dimensions} repeat, |
| 29 | + linear-gradient(135deg, ${color} 25%, transparent 25%) ${position3} / ${dimensions} repeat, |
| 30 | + linear-gradient(135deg, transparent 75%, ${color} 75%) ${position4} / ${dimensions} repeat |
| 31 | + `; |
| 32 | +}; |
| 33 | + |
| 34 | +const StyledDiv = styled.div<{ background: string }>` |
| 35 | + display: flex; |
| 36 | + align-items: center; |
| 37 | + justify-content: center; |
| 38 | + background: ${p => p.background}; |
27 | 39 | height: 208px; |
28 | 40 | `; |
29 | 41 |
|
30 | | -export const GetColorStory: Story<IArgs> = args => <StyledDiv {...args} />; |
| 42 | +interface IColorProps { |
| 43 | + dark?: object; |
| 44 | + hue?: string; |
| 45 | + light?: object; |
| 46 | + offset?: number; |
| 47 | + shade?: number; |
| 48 | + theme: IGardenTheme; |
| 49 | + transparency?: number; |
| 50 | + variable?: string; |
| 51 | +} |
| 52 | + |
| 53 | +const Color = ({ dark, hue, light, offset, shade, theme, transparency, variable }: IColorProps) => { |
| 54 | + let background; |
| 55 | + let tag; |
| 56 | + |
| 57 | + try { |
| 58 | + const backgroundColor = getColor({ |
| 59 | + dark, |
| 60 | + hue, |
| 61 | + light, |
| 62 | + offset, |
| 63 | + shade, |
| 64 | + theme, |
| 65 | + transparency: transparency ? transparency / 100 : undefined, |
| 66 | + variable |
| 67 | + }); |
| 68 | + |
| 69 | + background = toBackground(theme, backgroundColor); |
| 70 | + tag = ( |
| 71 | + <Tag hue={getColor({ theme, variable: 'background.default' })} size="large"> |
| 72 | + {backgroundColor} |
| 73 | + </Tag> |
| 74 | + ); |
| 75 | + } catch (error) { |
| 76 | + background = 'transparent'; |
| 77 | + tag = ( |
| 78 | + <Tag hue="red" size="large"> |
| 79 | + {error instanceof Error ? error.message : String(error)} |
| 80 | + </Tag> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return <StyledDiv background={background}>{tag}</StyledDiv>; |
| 85 | +}; |
| 86 | + |
| 87 | +interface IArgs extends Omit<IColorProps, 'theme'> { |
| 88 | + theme: { |
| 89 | + colors: Omit<IGardenTheme['colors'], 'base'>; |
| 90 | + palette: IGardenTheme['palette']; |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +export const GetColorStory: StoryFn<IArgs> = ({ |
| 95 | + dark, |
| 96 | + hue, |
| 97 | + light, |
| 98 | + offset, |
| 99 | + shade, |
| 100 | + theme: _theme, |
| 101 | + transparency, |
| 102 | + variable |
| 103 | +}) => { |
| 104 | + const parentTheme = useTheme(); |
| 105 | + const theme = { |
| 106 | + ...parentTheme, |
| 107 | + colors: { ..._theme.colors, base: parentTheme.colors.base }, |
| 108 | + palette: _theme.palette |
| 109 | + }; |
| 110 | + |
| 111 | + return ( |
| 112 | + <Color |
| 113 | + dark={dark} |
| 114 | + hue={hue} |
| 115 | + light={light} |
| 116 | + offset={offset} |
| 117 | + shade={shade} |
| 118 | + theme={theme} |
| 119 | + transparency={transparency} |
| 120 | + variable={variable} |
| 121 | + /> |
| 122 | + ); |
| 123 | +}; |
0 commit comments