|
1 |
| -import Color from 'tinycolor2' |
2 |
| -import { |
3 |
| - memoizedGet as get, |
4 |
| - fromEntries, |
5 |
| - Dict, |
6 |
| - isEmptyObject, |
7 |
| -} from '@chakra-ui/vue-utils' |
8 |
| - |
9 |
| -// TODO: rEMove this |
10 |
| -export function orient(options: { |
11 |
| - orientation?: 'vertical' | 'horizontal' |
12 |
| - vertical: any |
13 |
| - horizontal: any |
14 |
| -}) { |
15 |
| - const { orientation, vertical, horizontal } = options |
16 |
| - if (!orientation) return {} |
17 |
| - return orientation === 'vertical' ? vertical : horizontal |
18 |
| -} |
19 |
| - |
20 |
| -// TODO: rEMove this |
21 |
| -export type GlobalStyles = { |
22 |
| - global?: any |
23 |
| -} |
24 |
| - |
25 |
| -// TODO: rEMove this |
26 |
| -export type Styles = GlobalStyles |
27 |
| - |
28 |
| -export interface BaseBreakpointConfig extends Record<string, string> { |
29 |
| - sm: string |
30 |
| - md: string |
31 |
| - lg: string |
32 |
| - xl: string |
33 |
| -} |
34 |
| - |
35 |
| -export type Breakpoints<T = BaseBreakpointConfig> = string[] & WithBase<T> |
36 |
| -export type WithBase<T> = T & { base: '0em' } |
37 |
| - |
38 |
| -export const createBreakpoints = <T extends BaseBreakpointConfig>( |
39 |
| - config: T |
40 |
| -): Breakpoints<T> => { |
41 |
| - const sorted = fromEntries<WithBase<T>>( |
42 |
| - Object.entries({ base: '0em', ...config }).sort((a, b) => |
43 |
| - parseInt(a[1], 10) > parseInt(b[1], 10) ? 1 : -1 |
44 |
| - ) |
45 |
| - ) |
46 |
| - |
47 |
| - return Object.assign(Object.values(sorted), sorted) |
48 |
| -} |
49 |
| - |
50 |
| -export function mode(light: any, dark: any) { |
51 |
| - return (props: Dict) => (props.colorMode === 'dark' ? dark : light) |
52 |
| -} |
53 |
| - |
54 |
| -/** |
55 |
| - * Get the color raw value from theme |
56 |
| - * @param theme - the theme object |
57 |
| - * @param color - the color path ("green.200") |
58 |
| - * @param fallback - the fallback color |
59 |
| - */ |
60 |
| -export const getColor = (theme: Dict, color: string, fallback?: string) => { |
61 |
| - const hex = get(theme, `colors.${color}`, color) |
62 |
| - const isValid = Color(hex).isValid() |
63 |
| - return isValid ? hex : fallback |
64 |
| -} |
65 |
| - |
66 |
| -/** |
67 |
| - * Determines if the tone of given color is "light" or "dark" |
68 |
| - * @param color - the color in hex, rgb, or hsl |
69 |
| - */ |
70 |
| -export const tone = (color: string) => (theme: Dict) => { |
71 |
| - const hex = getColor(theme, color) |
72 |
| - const isDark = Color(hex).isDark() |
73 |
| - return isDark ? 'dark' : 'light' |
74 |
| -} |
75 |
| - |
76 |
| -/** |
77 |
| - * Determines if a color tone is "dark" |
78 |
| - * @param color - the color in hex, rgb, or hsl |
79 |
| - */ |
80 |
| -export const isDark = (color: string) => (theme: Dict) => |
81 |
| - tone(color)(theme) === 'dark' |
82 |
| - |
83 |
| -/** |
84 |
| - * Determines if a color tone is "light" |
85 |
| - * @param color - the color in hex, rgb, or hsl |
86 |
| - */ |
87 |
| -export const isLight = (color: string) => (theme: Dict) => |
88 |
| - tone(color)(theme) === 'light' |
89 |
| - |
90 |
| -/** |
91 |
| - * Make a color transparent |
92 |
| - * @param color - the color in hex, rgb, or hsl |
93 |
| - * @param amount - the amount white to add |
94 |
| - */ |
95 |
| -export const transparentize = (color: string, opacity: number) => ( |
96 |
| - theme: Dict |
97 |
| -) => { |
98 |
| - const raw = getColor(theme, color) |
99 |
| - return Color(raw).setAlpha(opacity).toRgbString() |
100 |
| -} |
101 |
| - |
102 |
| -/** |
103 |
| - * Add white to a color |
104 |
| - * @param color - the color in hex, rgb, or hsl |
105 |
| - * @param amount - the amount white to add (0-1) |
106 |
| - */ |
107 |
| -export const whiten = (color: string, amount: number) => (theme: Dict) => { |
108 |
| - const raw = getColor(theme, color) |
109 |
| - return Color.mix(raw, '#fff', amount).toHexString() |
110 |
| -} |
111 |
| - |
112 |
| -/** |
113 |
| - * Add black to a color |
114 |
| - * @param color - the color in hex, rgb, or hsl |
115 |
| - * @param amount - the amount white to add (0-1) |
116 |
| - */ |
117 |
| -export const blacken = (color: string, amount: number) => (theme: Dict) => { |
118 |
| - const raw = getColor(theme, color) |
119 |
| - return Color.mix(raw, '#000', amount).toHexString() |
120 |
| -} |
121 |
| - |
122 |
| -/** |
123 |
| - * Darken a specified color |
124 |
| - * @param color - the color in hex, rgb, or hsl |
125 |
| - * @param amount - the amount white to add (0-1) |
126 |
| - */ |
127 |
| -export const darken = (color: string, amount: number) => (theme: Dict) => { |
128 |
| - const raw = getColor(theme, color) |
129 |
| - return Color(raw).darken(amount).toHexString() |
130 |
| -} |
131 |
| - |
132 |
| -export const lighten = (color: string, amount: number) => (theme: Dict) => |
133 |
| - Color(getColor(theme, color)).lighten(amount).toHexString() |
134 |
| - |
135 |
| -/** |
136 |
| - * Checks the contract ratio of between 2 colors, |
137 |
| - * based on the Web Content Accessibility Guidelines (Version 2.0). |
138 |
| - * |
139 |
| - * @param fg - the foreground or text color |
140 |
| - * @param bg - the background color |
141 |
| - */ |
142 |
| -export const contrast = (fg: string, bg: string) => (theme: Dict) => |
143 |
| - Color.readability(getColor(theme, bg), getColor(theme, fg)) |
144 |
| - |
145 |
| -/** |
146 |
| - * Checks if a color meets the Web Content Accessibility |
147 |
| - * Guidelines (Version 2.0) for constract ratio. |
148 |
| - * |
149 |
| - * @param fg - the foreground or text color |
150 |
| - * @param bg - the background color |
151 |
| - */ |
152 |
| -export const isAccessible = ( |
153 |
| - textColor: string, |
154 |
| - bgColor: string, |
155 |
| - options?: Color.WCAG2Options |
156 |
| -) => (theme: Dict) => |
157 |
| - Color.isReadable( |
158 |
| - getColor(theme, bgColor), |
159 |
| - getColor(theme, textColor), |
160 |
| - options |
161 |
| - ) |
162 |
| - |
163 |
| -export const complementary = (color: string) => (theme: Dict) => |
164 |
| - Color(getColor(theme, color)).complement().toHexString() |
165 |
| - |
166 |
| -export function generateStripe( |
167 |
| - size = '1rem', |
168 |
| - color = 'rgba(255, 255, 255, 0.15)' |
169 |
| -) { |
170 |
| - return { |
171 |
| - backgroundImage: `linear-gradient( |
172 |
| - 45deg, |
173 |
| - ${color} 25%, |
174 |
| - transparent 25%, |
175 |
| - transparent 50%, |
176 |
| - ${color} 50%, |
177 |
| - ${color} 75%, |
178 |
| - transparent 75%, |
179 |
| - transparent |
180 |
| - )`, |
181 |
| - backgroundSize: `${size} ${size}`, |
182 |
| - } |
183 |
| -} |
184 |
| - |
185 |
| -interface RandomColorOptions { |
186 |
| - /** |
187 |
| - * If passed, string will be used to generate |
188 |
| - * random color |
189 |
| - */ |
190 |
| - string?: string |
191 |
| - /** |
192 |
| - * List of colors to pick from at random |
193 |
| - */ |
194 |
| - colors?: string[] |
195 |
| -} |
196 |
| - |
197 |
| -export function randomColor(opts?: RandomColorOptions) { |
198 |
| - const fallback = Color.random().toHexString() |
199 |
| - |
200 |
| - if (!opts || isEmptyObject(opts)) { |
201 |
| - return fallback |
202 |
| - } |
203 |
| - |
204 |
| - if (opts.string && opts.colors) { |
205 |
| - return randomColorFromList(opts.string, opts.colors) |
206 |
| - } |
207 |
| - |
208 |
| - if (opts.string && !opts.colors) { |
209 |
| - return randomColorFromString(opts.string) |
210 |
| - } |
211 |
| - |
212 |
| - if (opts.colors && !opts.string) { |
213 |
| - return randomFromList(opts.colors) |
214 |
| - } |
215 |
| - |
216 |
| - return fallback |
217 |
| -} |
218 |
| - |
219 |
| -function randomColorFromString(str: string) { |
220 |
| - let hash = 0 |
221 |
| - if (str.length === 0) return hash.toString() |
222 |
| - for (let i = 0; i < str.length; i += 1) { |
223 |
| - hash = str.charCodeAt(i) + ((hash << 5) - hash) |
224 |
| - hash = hash & hash |
225 |
| - } |
226 |
| - let color = '#' |
227 |
| - for (let j = 0; j < 3; j += 1) { |
228 |
| - const value = (hash >> (j * 8)) & 255 |
229 |
| - color += `00${value.toString(16)}`.substr(-2) |
230 |
| - } |
231 |
| - return color |
232 |
| -} |
233 |
| - |
234 |
| -function randomColorFromList(str: string, list: string[]) { |
235 |
| - let index = 0 |
236 |
| - if (str.length === 0) return list[0] |
237 |
| - for (let i = 0; i < str.length; i += 1) { |
238 |
| - index = str.charCodeAt(i) + ((index << 5) - index) |
239 |
| - index = index & index |
240 |
| - } |
241 |
| - index = ((index % list.length) + list.length) % list.length |
242 |
| - return list[index] |
243 |
| -} |
244 |
| - |
245 |
| -function randomFromList(list: string[]) { |
246 |
| - return list[Math.floor(Math.random() * list.length)] |
247 |
| -} |
| 1 | +export * from './color' |
| 2 | +export * from './component' |
| 3 | +export * from './create-breakpoints' |
0 commit comments