@lumen-design/color-picker 0.0.2
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.
- package/dist/ColorPicker.svelte +112 -0
- package/dist/index.js +3 -0
- package/dist/types.js +1 -0
- package/package.json +35 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ColorPickerSize } from './types'
|
|
3
|
+
import Icon from '@lumen-design/icon'
|
|
4
|
+
import { ChevronDown } from 'lucide'
|
|
5
|
+
import { onMount, onDestroy } from 'svelte'
|
|
6
|
+
import { createDropdownTransition } from '../../utils'
|
|
7
|
+
|
|
8
|
+
interface ColorPickerProps {
|
|
9
|
+
modelValue?: string
|
|
10
|
+
disabled?: boolean
|
|
11
|
+
size?: ColorPickerSize
|
|
12
|
+
showAlpha?: boolean
|
|
13
|
+
predefine?: string[]
|
|
14
|
+
class?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let { modelValue = $bindable('#409eff'), disabled = false, size = 'default', showAlpha = false, predefine = [], class: cls = '', ...attrs }: ColorPickerProps = $props()
|
|
18
|
+
|
|
19
|
+
let visible = $state(false)
|
|
20
|
+
let tempValue = $state(modelValue)
|
|
21
|
+
let reduceMotion = $state(false)
|
|
22
|
+
let pickerRef: HTMLDivElement | null = null
|
|
23
|
+
|
|
24
|
+
const handleClickOutside = (e: MouseEvent): void => {
|
|
25
|
+
if (visible && pickerRef && !pickerRef.contains(e.target as Node)) {
|
|
26
|
+
visible = false
|
|
27
|
+
tempValue = modelValue
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const toggle = (): void => {
|
|
32
|
+
if (disabled) return
|
|
33
|
+
visible = !visible
|
|
34
|
+
if (visible) {
|
|
35
|
+
tempValue = modelValue
|
|
36
|
+
} else {
|
|
37
|
+
tempValue = modelValue
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const selectColor = (color: string): void => {
|
|
42
|
+
tempValue = color
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const handleInputChange = (e: Event): void => {
|
|
46
|
+
const target = e.target as HTMLInputElement
|
|
47
|
+
tempValue = target.value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const cancel = (): void => {
|
|
51
|
+
tempValue = modelValue
|
|
52
|
+
visible = false
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const confirm = (): void => {
|
|
56
|
+
modelValue = tempValue
|
|
57
|
+
visible = false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
onMount(() => {
|
|
61
|
+
document.addEventListener('click', handleClickOutside)
|
|
62
|
+
if (typeof window !== 'undefined') {
|
|
63
|
+
reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches ?? false
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
onDestroy(() => {
|
|
68
|
+
document.removeEventListener('click', handleClickOutside)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const classes = $derived(`lm-color-picker lm-color-picker--${size}${disabled ? ' is-disabled' : ''}${cls ? ` ${cls}` : ''}`)
|
|
72
|
+
|
|
73
|
+
const dropdownTransition = $derived(createDropdownTransition(reduceMotion))
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<div class={classes} bind:this={pickerRef} {...attrs}>
|
|
77
|
+
<button type="button" class="lm-color-picker__trigger" class:is-focus={visible} onclick={toggle} {disabled}>
|
|
78
|
+
<span class="lm-color-picker__color" style="background-color:{modelValue}"></span>
|
|
79
|
+
<span class="lm-color-picker__icon" class:is-open={visible} aria-hidden="true">
|
|
80
|
+
<Icon icon={ChevronDown} size={14} />
|
|
81
|
+
</span>
|
|
82
|
+
</button>
|
|
83
|
+
|
|
84
|
+
{#if visible}
|
|
85
|
+
<div class="lm-color-picker__dropdown" transition:dropdownTransition>
|
|
86
|
+
<div class="lm-color-picker__panel">
|
|
87
|
+
<input type="color" class="lm-color-picker__input" value={tempValue} oninput={handleInputChange} />
|
|
88
|
+
|
|
89
|
+
{#if predefine.length > 0}
|
|
90
|
+
<div class="lm-color-picker__predefine">
|
|
91
|
+
{#each predefine as color (color)}
|
|
92
|
+
<span
|
|
93
|
+
class="lm-color-picker__predefine-color{tempValue === color ? ' is-active' : ''}"
|
|
94
|
+
style="background-color:{color}"
|
|
95
|
+
onclick={() => selectColor(color)}
|
|
96
|
+
role="button"
|
|
97
|
+
tabindex="0"
|
|
98
|
+
onkeydown={(e: KeyboardEvent) => e.key === 'Enter' && selectColor(color)}
|
|
99
|
+
></span>
|
|
100
|
+
{/each}
|
|
101
|
+
</div>
|
|
102
|
+
{/if}
|
|
103
|
+
|
|
104
|
+
<div class="lm-color-picker__footer">
|
|
105
|
+
<input type="text" class="lm-color-picker__hex" value={tempValue} oninput={handleInputChange} />
|
|
106
|
+
<button type="button" class="lm-color-picker__btn lm-color-picker__btn--cancel" onclick={cancel}>取消</button>
|
|
107
|
+
<button type="button" class="lm-color-picker__btn lm-color-picker__btn--confirm" onclick={confirm}>确定</button>
|
|
108
|
+
</div>
|
|
109
|
+
</div>
|
|
110
|
+
</div>
|
|
111
|
+
{/if}
|
|
112
|
+
</div>
|
package/dist/index.js
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lumen-design/color-picker",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "ColorPicker component for Lumen UI",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"svelte": "./dist/index.js",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "svelte-package -i src -o dist --types",
|
|
21
|
+
"build:watch": "svelte-package -i src -o dist --types -w"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@lumen-design/icon": "0.0.2",
|
|
25
|
+
"lucide": "^0.563.0",
|
|
26
|
+
"@lumen-design/utils": "0.0.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@sveltejs/package": "^2.5.7",
|
|
30
|
+
"svelte": "5.48.2"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"svelte": "^5.0.0"
|
|
34
|
+
}
|
|
35
|
+
}
|