diff --git a/README.md b/README.md index 5bfc0e8..15fbe51 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # colorer -Play around with hsla color +Play around with hsla color! + +Try it online: https://colorer.vercel.app/ ## Usage diff --git a/src/App.tsx b/src/App.tsx index c462650..e48983b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,59 +1,14 @@ -import { createSignal } from "solid-js"; +import { HslaProvider } from "/@/hsla-context"; +import Colorer from "/@lib/colorer"; -function Colorer(props) { - const [h, setH] = createSignal(1); - const [s, setS] = createSignal(50); - const [l, setL] = createSignal(50); - const [a, setA] = createSignal(1); - const hslColor = () => `hsla(${h()}, ${s()}%, ${l()}%, ${a()})`; +const startHsla = { h: 1, s: 50, l: 50, a: 1 }; - const changeH = (e) => setH(e.currentTarget.value); - const changeS = (e) => setS(e.currentTarget.value); - const changeL = (e) => setL(e.currentTarget.value); - const changeA = (e) => setA(e.currentTarget.value); - - return ( -
-
- - - - -
-
-
- ); -} - -function App() { - return ( - <> -
-

colorer

-
-
- -
- - ); -}; - -export default App; +export default () => + +
+

colorer

+
+
+ +
+
diff --git a/src/components/color-input.tsx b/src/components/color-input.tsx new file mode 100644 index 0000000..f91f5cf --- /dev/null +++ b/src/components/color-input.tsx @@ -0,0 +1,26 @@ +import { mergeProps } from "solid-js"; + +function ColorInput(props) { + const options = mergeProps({ step: 1 }, props); + + const setValue = (e) => { + let value = parseFloat(e.currentTarget.value); + options.onInput(value); + } + + return ( +
+ +
+ ) +} + +export default ColorInput; diff --git a/src/components/colorer.tsx b/src/components/colorer.tsx new file mode 100644 index 0000000..bfa7b37 --- /dev/null +++ b/src/components/colorer.tsx @@ -0,0 +1,25 @@ +import { useHslaContext } from "/@/hsla-context"; +import ColorInput from "/@lib/color-input"; + +function Colorer(props) { + const [hsla, { setH, setS, setL, setA }] = useHslaContext(); + + const hslColor = () => `hsla(${hsla().h}, ${hsla().s}%, ${hsla().l}%, ${hsla().a})`; + + return ( +
+
+ + + + +
+
+
{hslColor()}
+
+ ); +} + +export default Colorer; diff --git a/src/hsla-context.tsx b/src/hsla-context.tsx new file mode 100644 index 0000000..06d654f --- /dev/null +++ b/src/hsla-context.tsx @@ -0,0 +1,53 @@ +import { createSignal, createEffect, createContext, useContext } from "solid-js"; + +const HslaContext = createContext(); + +export function HslaProvider(props) { + const defaultHsla = { h: 1, s: 50, l: 50, a: 1 }; + const [hsla, setHsla] = createSignal(props.hsla || defaultHsla), + store = [ + hsla, + { + setHsla(value) { + setHsla(value) + }, + setH(value) { + setHsla({ ...hsla(), h: value }) + }, + setS(value) { + setHsla({ ...hsla(), s: value }) + }, + setL(value) { + setHsla({ ...hsla(), l: value }) + }, + setA(value) { + setHsla({ ...hsla(), a: value }) + } + } + ]; + + return ( + + {props.children} + + ); +} + +export function useHslaContext() { + const context = useContext(HslaContext); + const storageName = "hsla" + const [hsla, { setHsla, ...rest }] = context; + + if (window.localStorage) { + const storedHsla = window.localStorage.getItem(storageName); + if (storedHsla != null) { + setHsla(JSON.parse(storedHsla)) + } + + createEffect(() => { + window.localStorage.setItem(storageName, JSON.stringify(hsla())); + }); + } + + return context; +} diff --git a/src/index.css b/src/index.css index 793b2fe..adfe581 100644 --- a/src/index.css +++ b/src/index.css @@ -12,6 +12,7 @@ body { -moz-osx-font-smoothing: grayscale; @apply bg-gray-700; height: 100vh; + font-size: 12pt; } code { @@ -30,21 +31,19 @@ code { "main"; } -#colorer { - @apply flex flex-col; -} - header { grid-area: header; color: white; } header h1 { @apply font-mono text-4xl text-center mt-2; } main { - @apply bg-white m-16 mx-auto p-4; + @apply bg-white mt-8 mx-auto p-4 pb-5; @apply flex justify-center content-center; grid-area: main; width: min(50%, 75vw); height: min(75%, 50vh); } +main pre { @apply float-right; } + .main-container { width: 100%; } label { @apply bg-white p-1 pl-0; } @@ -53,8 +52,9 @@ label > input { @apply font-mono text-right; } #colorer { float: left; } -input[type=number] { - width: 6ch; -} +.color-input label { @apply flex gap-0.5; } +.color-input label span { @apply inline-block w-4; } +.color-input input[type=number] { width: 6ch; } +.color-input input[type=range] { @apply flex-shrink; } .color-box { width: 100%; height: 100%; }