Skip to content

Commit 34029ba

Browse files
committed
Add HSL sliders
1 parent 681592e commit 34029ba

File tree

3 files changed

+103
-16
lines changed

3 files changed

+103
-16
lines changed

docs/_react/bulma-customizer/src/App.jsx

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,28 @@ import "../../../../css/bulma.css";
33
import "./App.css";
44
import Slider from "./components/Slider";
55

6-
// const COLORS = ["primary", "link", "info", "success", "warning", "danger"];
6+
const COLORS = ["primary", "link", "info", "success", "warning", "danger"];
77

88
const KEYS = [
99
"scheme-h",
1010
"primary-h",
1111
"primary-s",
1212
"primary-l",
13+
"link-h",
14+
"link-s",
15+
"link-l",
16+
"info-h",
17+
"info-s",
18+
"info-l",
19+
"success-h",
20+
"success-s",
21+
"success-l",
22+
"warning-h",
23+
"warning-s",
24+
"warning-l",
25+
"danger-h",
26+
"danger-s",
27+
"danger-l",
1328
"skeleton-lines-gap",
1429
];
1530
const UNITS = ["deg", "rem", "em", "%"];
@@ -21,20 +36,22 @@ const SUFFIX_TO_KIND = {
2136
};
2237

2338
function App() {
24-
const [vars, setVars] = useState([]);
39+
const [vars, setVars] = useState({});
2540

2641
useEffect(() => {
2742
const rootStyle = window.getComputedStyle(document.documentElement);
2843

29-
const cssvars = KEYS.map((key) => {
44+
const cssvars = {};
45+
46+
KEYS.map((key) => {
3047
const original = rootStyle.getPropertyValue(`--bulma-${key}`);
3148
const suffix = Object.keys(SUFFIX_TO_KIND).find((kind) =>
3249
key.endsWith(kind),
3350
);
3451
const unit = UNITS.find((unit) => original.endsWith(unit)) || "";
3552
const value = unit !== "" ? original.split(unit)[0] : original;
3653

37-
return {
54+
cssvars[key] = {
3855
id: key,
3956
kind: SUFFIX_TO_KIND[suffix] || "any",
4057
original,
@@ -46,13 +63,55 @@ function App() {
4663
setVars(cssvars);
4764
}, []);
4865

49-
console.log("ZLOG vars", vars);
50-
5166
return (
5267
<section className="section">
5368
<div className="card">
5469
<div className="card-content">
55-
{vars.map((v) => {
70+
{COLORS.map((color) => {
71+
const h = `${color}-h`;
72+
73+
if (!(h in vars)) {
74+
return;
75+
}
76+
77+
const s = `${color}-s`;
78+
const l = `${color}-l`;
79+
80+
return (
81+
<div key={color} className="block">
82+
<code>{color}</code>
83+
84+
<Slider
85+
id={h}
86+
kind="hue"
87+
color={color}
88+
original={vars[h].original}
89+
start={vars[h].start}
90+
unit={vars[h].unit}
91+
/>
92+
93+
<Slider
94+
id={s}
95+
kind="saturation"
96+
color={color}
97+
original={vars[s].original}
98+
start={vars[s].start}
99+
unit={vars[s].unit}
100+
/>
101+
102+
<Slider
103+
id={l}
104+
kind="lightness"
105+
color={color}
106+
original={vars[l].original}
107+
start={vars[l].start}
108+
unit={vars[l].unit}
109+
/>
110+
</div>
111+
);
112+
})}
113+
114+
{/* {vars.map((v) => {
56115
const { id, kind, original, unit, start } = v;
57116
58117
return (
@@ -67,14 +126,12 @@ function App() {
67126
/>
68127
</div>
69128
);
70-
})}
129+
})} */}
71130

72131
<div className="buttons">
73132
<button className="button">Button</button>
74-
75133
<button className="button is-primary">Primary</button>
76134
<button className="button is-link">Link</button>
77-
78135
<button className="button is-info">Info</button>
79136
<button className="button is-success">Success</button>
80137
<button className="button is-warning">Warning</button>

docs/_react/bulma-customizer/src/components/Slider.jsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ const valueToX = (value, width, min, max) => {
2424
return Math.round(newValue);
2525
};
2626

27-
function Slider({ id, kind, start, unit }) {
27+
function Slider({ id, color, kind, start, unit }) {
2828
const [min, max] = RANGES[kind];
2929

30+
const sliderRef = useRef(null);
31+
const handleRef = useRef(null);
32+
3033
const [value, setValue] = useState(start);
3134
const [isMoving, setMoving] = useState(false);
3235
const [x, setX] = useState(valueToX(start, 240, min, max));
33-
const sliderRef = useRef(null);
34-
const handleRef = useRef(null);
3536

3637
const handleMouseDown = (event) => {
3738
setMoving(true);
@@ -121,12 +122,23 @@ function Slider({ id, kind, start, unit }) {
121122
[cn[kind]]: true,
122123
});
123124

125+
const mainStyle = {
126+
"--h": `var(--bulma-${color}-h)`,
127+
"--s": `var(--bulma-${color}-s)`,
128+
"--l": `var(--bulma-${color}-l)`,
129+
};
130+
124131
const handleStyle = {
125132
transform: `translateX(${x}px)`,
126133
};
127134

128135
return (
129-
<div className={mainCN} ref={sliderRef} onMouseDown={handleMouseDown}>
136+
<div
137+
className={mainCN}
138+
ref={sliderRef}
139+
style={mainStyle}
140+
onMouseDown={handleMouseDown}
141+
>
130142
<div className={backgroundCN}>
131143
<span ref={handleRef} className={cn.handle} style={handleStyle} />
132144
</div>
@@ -137,6 +149,7 @@ function Slider({ id, kind, start, unit }) {
137149
Slider.propTypes = {
138150
id: PropTypes.string,
139151
kind: PropTypes.string,
152+
color: PropTypes.string,
140153
original: PropTypes.string,
141154
start: PropTypes.number,
142155
unit: PropTypes.string,

docs/_react/bulma-customizer/src/components/Slider.module.css

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
position: relative;
33
width: 15rem;
44
padding: 0.375rem 0;
5-
box-shadow: 0 0 0 1px green;
65
cursor: grab;
76
}
87

@@ -31,7 +30,7 @@
3130
}
3231

3332
.background {
34-
border-radius: 0.125rem;
33+
border-radius: 0.25rem;
3534
background-color: white;
3635
height: 0.5rem;
3736
}
@@ -48,3 +47,21 @@
4847
rgb(255, 0, 0)
4948
);
5049
}
50+
51+
.saturation {
52+
background-image: linear-gradient(
53+
to right,
54+
hsl(var(--h), 0%, var(--l)),
55+
hsl(var(--h), var(--s), var(--l)),
56+
hsl(var(--h), 100%, var(--l))
57+
);
58+
}
59+
60+
.lightness {
61+
background-image: linear-gradient(
62+
to right,
63+
hsl(var(--h), var(--s), 0%),
64+
hsl(var(--h), var(--s), 50%),
65+
hsl(var(--h), var(--s), 100%)
66+
);
67+
}

0 commit comments

Comments
 (0)