|
1 | 1 | import '.';
|
2 | 2 | import readme from '../README.md?raw';
|
3 | 3 | import { html } from 'lit';
|
| 4 | +import { ifDefined } from 'lit/directives/if-defined.js'; |
| 5 | +import { styleMap } from 'lit/directives/style-map.js'; |
4 | 6 | import type { Meta, StoryObj } from '@storybook/web-components';
|
| 7 | +import { useState } from '@storybook/preview-api'; |
5 | 8 | import { spread } from '../../../storyhelpers';
|
| 9 | +import { repeat } from 'lit/directives/repeat.js'; |
| 10 | + |
| 11 | +import { colord, HslaColor } from 'colord'; |
| 12 | + |
| 13 | +import type { UUIColorSliderElement } from '@umbraco-ui/uui-color-slider/lib'; |
6 | 14 |
|
7 | 15 | const meta: Meta = {
|
8 | 16 | id: 'uui-color-slider',
|
9 | 17 | component: 'uui-color-slider',
|
10 | 18 | title: 'Inputs/Color/Color Slider',
|
11 | 19 | argTypes: {
|
12 | 20 | type: {
|
13 |
| - options: ['hue', 'opacity'], |
| 21 | + options: ['hue', 'opacity', 'saturation', 'lightness'], |
14 | 22 | control: { type: 'select' },
|
15 | 23 | },
|
16 | 24 | },
|
@@ -53,3 +61,173 @@ export const Vertical: Story = {
|
53 | 61 | vertical: true,
|
54 | 62 | },
|
55 | 63 | };
|
| 64 | + |
| 65 | +export const Advanced: Story = { |
| 66 | + render: () => { |
| 67 | + const sliders = [ |
| 68 | + { |
| 69 | + label: 'H', |
| 70 | + type: 'hue', |
| 71 | + color: '#0075ff', |
| 72 | + value: 0, |
| 73 | + min: 0, |
| 74 | + max: 360, |
| 75 | + }, |
| 76 | + { |
| 77 | + label: 'S', |
| 78 | + type: 'saturation', |
| 79 | + color: '#0075ff', |
| 80 | + value: 100, |
| 81 | + min: 0, |
| 82 | + max: 100, |
| 83 | + }, |
| 84 | + { |
| 85 | + label: 'L', |
| 86 | + type: 'lightness', |
| 87 | + color: '#0075ff', |
| 88 | + value: 50, |
| 89 | + min: 0, |
| 90 | + max: 100, |
| 91 | + }, |
| 92 | + { |
| 93 | + label: 'A', |
| 94 | + type: 'opacity', |
| 95 | + color: '#0075ff', |
| 96 | + value: 1, |
| 97 | + min: 0, |
| 98 | + max: 1, |
| 99 | + precision: 2, |
| 100 | + }, |
| 101 | + ]; |
| 102 | + |
| 103 | + const [value, setValue] = useState({ h: 0, s: 100, l: 50, a: 1 }); |
| 104 | + |
| 105 | + function handleSliderChange(e: Event, slider: any) { |
| 106 | + e.stopPropagation(); |
| 107 | + |
| 108 | + const element = e.target as UUIColorSliderElement; |
| 109 | + |
| 110 | + if (isNaN(element.value)) return; |
| 111 | + |
| 112 | + const newColor: HslaColor = { |
| 113 | + h: value.h, |
| 114 | + s: value.s, |
| 115 | + l: value.l, |
| 116 | + a: value.a, |
| 117 | + }; |
| 118 | + |
| 119 | + if (slider.type === 'hue') { |
| 120 | + newColor.h = element.value; |
| 121 | + } else if (slider.type === 'saturation') { |
| 122 | + newColor.s = element.value; |
| 123 | + } else if (slider.type === 'lightness') { |
| 124 | + newColor.l = element.value; |
| 125 | + } else if (slider.type === 'opacity') { |
| 126 | + newColor.a = element.value; |
| 127 | + } |
| 128 | + |
| 129 | + slider.value = element.value; |
| 130 | + |
| 131 | + setValue({ h: newColor.h, s: newColor.s, l: newColor.l, a: newColor.a }); |
| 132 | + } |
| 133 | + |
| 134 | + function handleInputChange(e: Event, slider: any) { |
| 135 | + e.stopPropagation(); |
| 136 | + |
| 137 | + const input = e.target as HTMLInputElement; |
| 138 | + |
| 139 | + let newValue = parseFloat(input.value); |
| 140 | + |
| 141 | + if (isNaN(newValue)) { |
| 142 | + newValue = 0; |
| 143 | + input.value = '0'; |
| 144 | + } |
| 145 | + |
| 146 | + const newColor: HslaColor = { |
| 147 | + h: value.h, |
| 148 | + s: value.s, |
| 149 | + l: value.l, |
| 150 | + a: value.a, |
| 151 | + }; |
| 152 | + |
| 153 | + if (slider.type === 'hue') { |
| 154 | + newColor.h = newValue; |
| 155 | + } else if (slider.type === 'saturation') { |
| 156 | + newColor.s = newValue; |
| 157 | + } else if (slider.type === 'lightness') { |
| 158 | + newColor.l = newValue; |
| 159 | + } else if (slider.type === 'opacity') { |
| 160 | + newColor.a = newValue; |
| 161 | + } |
| 162 | + |
| 163 | + slider.value = newValue; |
| 164 | + |
| 165 | + setValue({ h: newColor.h, s: newColor.s, l: newColor.l, a: newColor.a }); |
| 166 | + } |
| 167 | + |
| 168 | + /** Generates a hex string from HSL values. Hue must be 0-360. All other arguments must be 0-100. */ |
| 169 | + function getHexString( |
| 170 | + hue: number, |
| 171 | + saturation: number, |
| 172 | + lightness: number, |
| 173 | + alpha = 100, |
| 174 | + ) { |
| 175 | + const color = colord( |
| 176 | + `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha / 100})`, |
| 177 | + ); |
| 178 | + if (!color.isValid()) { |
| 179 | + return ''; |
| 180 | + } |
| 181 | + |
| 182 | + return color.toHex(); |
| 183 | + } |
| 184 | + |
| 185 | + return html` <div style="display: flex; gap: 20px;"> |
| 186 | + <div style="display: flex; flex-direction: column; gap: 10px;"> |
| 187 | + ${repeat(sliders, (slider: any) => { |
| 188 | + return html`<div style="display: flex; gap: 10px 20px;"> |
| 189 | + <label>${slider.label}</label> |
| 190 | + <uui-color-slider |
| 191 | + .type=${slider.type} |
| 192 | + .value=${slider.value} |
| 193 | + .min=${slider.min} |
| 194 | + .max=${slider.max} |
| 195 | + .color=${slider.type !== 'hue' |
| 196 | + ? getHexString(value.h, value.s, value.l) |
| 197 | + : undefined} |
| 198 | + ?precision=${ifDefined(slider.precision)} |
| 199 | + @change=${(e: Event) => handleSliderChange(e, slider)} |
| 200 | + style=${styleMap({ |
| 201 | + '--uui-slider-background-image': |
| 202 | + slider.type === 'saturation' |
| 203 | + ? `linear-gradient(to right, hsl(${value.h}, 0%, ${value.l}%), hsl(${value.h}, 100%, ${value.l}%))` |
| 204 | + : slider.type === 'lightness' |
| 205 | + ? `linear-gradient(to right, hsl(${value.h}, ${value.s}%, 0%), hsl(${value.h}, ${value.s}%, ${slider.value}%))` |
| 206 | + : undefined, |
| 207 | + width: '400px', |
| 208 | + })}> |
| 209 | + </uui-color-slider> |
| 210 | + <uui-input |
| 211 | + type="number" |
| 212 | + .min=${slider.min} |
| 213 | + .max=${slider.max} |
| 214 | + .step=${slider.precision > 1 ? slider.max / 10 : 1} |
| 215 | + .value=${slider.value} |
| 216 | + @change=${(e: Event) => handleInputChange(e, slider)} |
| 217 | + style="width: 60px;"> |
| 218 | + </uui-input> |
| 219 | + </div>`; |
| 220 | + })} |
| 221 | + </div> |
| 222 | + <div |
| 223 | + style="width: 100px; height: 100px; |
| 224 | + border: 1px solid var(--uui-color-border-standalone); |
| 225 | + background-image: linear-gradient(45deg, var(--uui-palette-grey) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, var(--uui-palette-grey) 75%), linear-gradient(45deg, transparent 75%, var(--uui-palette-grey) 75%), linear-gradient(45deg, var(--uui-palette-grey) 25%, transparent 25%); |
| 226 | + background-size: 10px 10px; |
| 227 | + background-position: 0 0, 0 0, -5px -5px, 5px 5px;"> |
| 228 | + <div |
| 229 | + style="width: 100%; height: 100%; background-color: hsla(${value.h}, ${value.s}%, ${value.l}%, ${value.a});"></div> |
| 230 | + </div> |
| 231 | + </div>`; |
| 232 | + }, |
| 233 | +}; |
0 commit comments