@@ -8,7 +8,7 @@ import { useState } from '@storybook/preview-api';
88import { spread } from '../../../storyhelpers' ;
99import { repeat } from 'lit/directives/repeat.js' ;
1010
11- import { HslaColor } from 'colord' ;
11+ import { colord , HslaColor } from 'colord' ;
1212
1313import type { UUIColorSliderElement } from '@umbraco-ui/uui-color-slider/lib' ;
1414
@@ -65,7 +65,14 @@ export const Vertical: Story = {
6565export const Advanced : Story = {
6666 render : ( ) => {
6767 const sliders = [
68- { label : 'H' , type : 'hue' , color : '#0075ff' , value : 0 , min : 0 , max : 360 } ,
68+ {
69+ label : 'H' ,
70+ type : 'hue' ,
71+ color : '#0075ff' ,
72+ value : 0 ,
73+ min : 0 ,
74+ max : 360 ,
75+ } ,
6976 {
7077 label : 'S' ,
7178 type : 'saturation' ,
@@ -82,7 +89,15 @@ export const Advanced: Story = {
8289 min : 0 ,
8390 max : 100 ,
8491 } ,
85- { label : 'A' , type : 'opacity' , value : 1 , min : 0 , max : 1 , precision : 2 } ,
92+ {
93+ label : 'A' ,
94+ type : 'opacity' ,
95+ color : '#0075ff' ,
96+ value : 1 ,
97+ min : 0 ,
98+ max : 1 ,
99+ precision : 2 ,
100+ } ,
86101 ] ;
87102
88103 const [ value , setValue ] = useState ( { h : 0 , s : 100 , l : 50 , a : 1 } ) ;
@@ -147,50 +162,69 @@ export const Advanced: Story = {
147162 setValue ( { h : newColor . h , s : newColor . s , l : newColor . l , a : newColor . a } ) ;
148163 }
149164
150- return html ` ${ value . h }
151- < div style ="display: flex; gap: 20px; ">
152- < div style ="display: flex; flex-direction: column; gap: 10px; ">
153- ${ repeat ( sliders , ( slider : any ) => {
154- return html `< div style ="display: flex; gap: 10px 20px; ">
155- < label > ${ slider . label } </ label >
156- < uui-color-slider
157- .type =${ slider . type }
158- .value =${ slider . value }
159- .min=${ slider . min }
160- .max=${ slider . max }
161- ?precision=${ ifDefined ( slider . precision ) }
162- @change=${ ( e : Event ) => handleSliderChange ( e , slider ) }
163- style=${ styleMap ( {
164- '--uui-slider-background-image' :
165- slider . type === 'saturation'
166- ? `linear-gradient(to right, hsl(${ value . h } , 0%, ${ value . l } %), hsl(${ value . h } , 100%, ${ value . l } %))`
167- : slider . type === 'lightness'
168- ? `linear-gradient(to right, hsl(${ value . h } , ${ value . s } %, 0%), hsl(${ value . h } , ${ value . s } %, ${ slider . value } %))`
169- : undefined ,
170- width : '400px' ,
171- } ) } >
172- </ uui-color-slider >
173- < uui-input
174- type ="number "
175- .min =${ slider . min }
176- .max =${ slider . max }
177- .step=${ slider . precision > 1 ? slider . max / 10 : 1 }
178- .value=${ slider . value }
179- @change=${ ( e : Event ) => handleInputChange ( e , slider ) }
180- style="width: 60px;">
181- </ uui-input >
182- </ div > ` ;
183- } ) }
184- </ div >
185- < div
186- style ="width: 100px; height: 100px;
165+ /** Generates a hex string from HSL values. Hue must be 0-360. All other arguments must be 0-100. */
166+ function getHexString (
167+ hue : number ,
168+ saturation : number ,
169+ lightness : number ,
170+ alpha = 100 ,
171+ ) {
172+ const color = colord (
173+ `hsla(${ hue } , ${ saturation } %, ${ lightness } %, ${ alpha / 100 } )` ,
174+ ) ;
175+ if ( ! color . isValid ( ) ) {
176+ return '' ;
177+ }
178+
179+ return color . toHex ( ) ;
180+ }
181+
182+ return html ` < div style ="display: flex; gap: 20px; ">
183+ < div style ="display: flex; flex-direction: column; gap: 10px; ">
184+ ${ repeat ( sliders , ( slider : any ) => {
185+ return html `< div style ="display: flex; gap: 10px 20px; ">
186+ < label > ${ slider . label } </ label >
187+ < uui-color-slider
188+ .type =${ slider . type }
189+ .value =${ slider . value }
190+ .min=${ slider . min }
191+ .max=${ slider . max }
192+ .color=${ slider . type === 'opacity'
193+ ? getHexString ( value . h , value . s , value . l )
194+ : undefined }
195+ ?precision=${ ifDefined ( slider . precision ) }
196+ @change=${ ( e : Event ) => handleSliderChange ( e , slider ) }
197+ style=${ styleMap ( {
198+ '--uui-slider-background-image' :
199+ slider . type === 'saturation'
200+ ? `linear-gradient(to right, hsl(${ value . h } , 0%, ${ value . l } %), hsl(${ value . h } , 100%, ${ value . l } %))`
201+ : slider . type === 'lightness'
202+ ? `linear-gradient(to right, hsl(${ value . h } , ${ value . s } %, 0%), hsl(${ value . h } , ${ value . s } %, ${ slider . value } %))`
203+ : undefined ,
204+ width : '400px' ,
205+ } ) } >
206+ </ uui-color-slider >
207+ < uui-input
208+ type ="number "
209+ .min =${ slider . min }
210+ .max =${ slider . max }
211+ .step=${ slider . precision > 1 ? slider . max / 10 : 1 }
212+ .value=${ slider . value }
213+ @change=${ ( e : Event ) => handleInputChange ( e , slider ) }
214+ style="width: 60px;">
215+ </ uui-input >
216+ </ div > ` ;
217+ } ) }
218+ </ div >
219+ < div
220+ style ="width: 100px; height: 100px;
187221 border: 1px solid var(--uui-color-border-standalone);
188222 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%);
189223 background-size: 10px 10px;
190224 background-position: 0 0, 0 0, -5px -5px, 5px 5px; ">
191- < div
192- style ="width: 100%; height: 100%; background-color: hsla(${ value . h } , ${ value . s } %, ${ value . l } %, ${ value . a } ); "> </ div >
193- </ div >
194- </ div > ` ;
225+ < div
226+ style ="width: 100%; height: 100%; background-color: hsla(${ value . h } , ${ value . s } %, ${ value . l } %, ${ value . a } ); "> </ div >
227+ </ div >
228+ </ div > ` ;
195229 } ,
196230} ;
0 commit comments