Add Color module

This commit is contained in:
Jeremy Thomas 2024-06-25 22:20:37 +01:00
parent cb2de3a2a2
commit 0014fc4e34
13 changed files with 183 additions and 98 deletions

View File

@ -1,54 +1,44 @@
import { createContext, useEffect, useState } from "react"; import { createContext, useEffect, useState } from "react";
import classNames from "classnames";
import Colors from "./pages/Colors";
import { CSSVAR_KEYS, SUFFIX_TO_KIND } from "./constants";
import { unslug } from "./utils";
import "../../../../css/bulma.css"; import "../../../../css/bulma.css";
import cn from "./App.module.css"; import Scheme from "./pages/Scheme";
import Color from "./components/Color";
const COLORS = ["primary", "link", "info", "success", "warning", "danger"];
const KEYS = [
"scheme-h",
"primary-h",
"primary-s",
"primary-l",
"link-h",
"link-s",
"link-l",
"info-h",
"info-s",
"info-l",
"success-h",
"success-s",
"success-l",
"warning-h",
"warning-s",
"warning-l",
"danger-h",
"danger-s",
"danger-l",
"skeleton-lines-gap",
];
const UNITS = ["deg", "rem", "em", "%"]; const UNITS = ["deg", "rem", "em", "%"];
const SUFFIX_TO_KIND = { const PAGE_TO_COMPONENT = {
"-h": "hue", colors: <Colors />,
"-s": "saturation", scheme: <Scheme />,
"-l": "lightness",
"-gap": "gap",
}; };
const PAGE_IDS = ["scheme", "colors"];
export const CustomizerContext = createContext({ export const CustomizerContext = createContext({
cssvars: {}, cssvars: {},
currentPage: "",
getVar: () => {}, getVar: () => {},
changeTab: () => {},
updateVar: () => {}, updateVar: () => {},
}); });
function App() { function App() {
const initialContext = { const initialContext = {
cssvars: {}, cssvars: {},
currentPage: "colors",
getVar: (id) => { getVar: (id) => {
return context.cssvars[id]; return context.cssvars[id];
}, },
changeTab: (pageId) => {
setContext((context) => {
return {
...context,
currentPage: pageId,
};
});
},
updateVar: (id, newValue) => { updateVar: (id, newValue) => {
setContext((context) => { setContext((context) => {
const { start, unit } = context.cssvars[id]; const { start, unit } = context.cssvars[id];
@ -78,12 +68,18 @@ function App() {
}; };
const [context, setContext] = useState(initialContext); const [context, setContext] = useState(initialContext);
const handleTabChange = (event, pageId) => {
event.preventDefault();
context.changeTab(pageId);
};
useEffect(() => { useEffect(() => {
const rootStyle = window.getComputedStyle(document.documentElement); const rootStyle = window.getComputedStyle(document.documentElement);
const cssvars = {}; const cssvars = {};
const allKeys = PAGE_IDS.map((pageId) => CSSVAR_KEYS[pageId]).flat();
KEYS.map((key) => { allKeys.map((key) => {
const original = rootStyle.getPropertyValue(`--bulma-${key}`); const original = rootStyle.getPropertyValue(`--bulma-${key}`);
const suffix = Object.keys(SUFFIX_TO_KIND).find((kind) => const suffix = Object.keys(SUFFIX_TO_KIND).find((kind) =>
key.endsWith(kind), key.endsWith(kind),
@ -109,43 +105,28 @@ function App() {
}); });
}, []); }, []);
// useEffect(() => {
// Object.values(context.cssvars).forEach((cssvar) => {
// const { id, current, unit } = cssvar;
// const computedValue = `${current}${unit}`;
// document.documentElement.style.setProperty(
// `--bulma-${id}`,
// computedValue,
// );
// });
// }, [context.cssvars]);
// useEffect(() => {
// const computedValue = `${current}${unit}`;
// if (current === start) {
// document.documentElement.style.removeProperty(`--bulma-${id}`);
// } else {
// document.documentElement.style.setProperty(
// `--bulma-${id}`,
// computedValue,
// );
// }
// }, [id, start, unit, value]);
return ( return (
<CustomizerContext.Provider value={context}> <CustomizerContext.Provider value={context}>
<section className="section"> <section className="section">
<div className="card"> <div className="buttons">
<div className="card-content"> {PAGE_IDS.map((pageId) => {
<div className={cn.colors}> const buttonClass = classNames({
{COLORS.map((color) => { button: true,
return <Color key={color} color={color} />; });
})}
</div> return (
</div> <button
className={buttonClass}
key={pageId}
onClick={(e) => handleTabChange(e, pageId)}
>
{unslug(pageId)}
</button>
);
})}
</div> </div>
{PAGE_TO_COMPONENT[context.currentPage]}
</section> </section>
</CustomizerContext.Provider> </CustomizerContext.Provider>
); );

View File

@ -1,2 +0,0 @@
.colors {
}

View File

@ -202,9 +202,19 @@ function Color({ color }) {
<p>{name}</p> <p>{name}</p>
</div> </div>
<button className="button is-small" onClick={handleHexInput}> <div className="buttons are-small">
Enter a Hex code <button className="button" onClick={handleHexInput}>
</button> Enter a Hex code
</button>
<button
className="button"
onClick={handleReset}
disabled={isDisabled}
>
Reset
</button>
</div>
<div className="is-hidden field has-addons"> <div className="is-hidden field has-addons">
<p className="control"> <p className="control">
@ -273,9 +283,6 @@ function Color({ color }) {
<div className={cn.side}> <div className={cn.side}>
<button className={`button is-${color}`}>{name}</button> <button className={`button is-${color}`}>{name}</button>
<button className="button" onClick={handleReset} disabled={isDisabled}>
Reset
</button>
</div> </div>
</div> </div>
); );

View File

@ -3,7 +3,7 @@
flex-wrap: wrap; flex-wrap: wrap;
gap: 1.5rem; gap: 1.5rem;
border-bottom: 1px solid var(--bulma-border); border-bottom: 1px solid var(--bulma-border);
padding: 1.25rem; padding: 1.25rem 0;
} }
.side { .side {

View File

@ -10,6 +10,7 @@ const RANGES = {
saturation: [0, 100, 1], saturation: [0, 100, 1],
lightness: [0, 100, 1], lightness: [0, 100, 1],
gap: [0, 100, 1], gap: [0, 100, 1],
delta: [0, 100, 1],
any: [0, 100, 1], any: [0, 100, 1],
}; };
@ -28,7 +29,7 @@ const valueToX = (value, width, min, max) => {
function Slider({ id, color, kind }) { function Slider({ id, color, kind }) {
const { cssvars, updateVar } = useContext(CustomizerContext); const { cssvars, updateVar } = useContext(CustomizerContext);
const { start, current } = cssvars[id]; const { start, current } = cssvars[id];
const [min, max] = RANGES[kind]; const [min, max] = kind ? RANGES[kind] : RANGES.any;
const sliderRef = useRef(null); const sliderRef = useRef(null);
const handleRef = useRef(null); const handleRef = useRef(null);
@ -129,11 +130,13 @@ function Slider({ id, color, kind }) {
[cn[kind]]: true, [cn[kind]]: true,
}); });
const mainStyle = { const mainStyle = color
"--h": `var(--bulma-${color}-h)`, ? {
"--s": `var(--bulma-${color}-s)`, "--h": `var(--bulma-${color}-h)`,
"--l": `var(--bulma-${color}-l)`, "--s": `var(--bulma-${color}-s)`,
}; "--l": `var(--bulma-${color}-l)`,
}
: {};
const handleStyle = { const handleStyle = {
transform: `translateX(${x}px)`, transform: `translateX(${x}px)`,

View File

@ -0,0 +1,51 @@
export const SUFFIX_TO_KIND = {
"-h": "hue",
"-s": "saturation",
"-l": "lightness",
"-gap": "gap",
"-delta": "delta",
};
export const CSSVAR_KEYS = {
scheme: [
"scheme-h",
"scheme-s",
"light-l",
"light-invert-l",
"dark-l",
"dark-invert-l",
"soft-l",
"bold-l",
"soft-invert-l",
"bold-invert-l",
"hover-background-l-delta",
"active-background-l-delta",
"hover-border-l-delta",
"active-border-l-delta",
"hover-color-l-delta",
"active-color-l-delta",
"hover-shadow-a-delta",
"active-shadow-a-delta",
],
colors: [
"primary-h",
"primary-s",
"primary-l",
"link-h",
"link-s",
"link-l",
"info-h",
"info-s",
"info-l",
"success-h",
"success-s",
"success-l",
"warning-h",
"warning-s",
"warning-l",
"danger-h",
"danger-s",
"danger-l",
"skeleton-lines-gap",
],
};

View File

@ -0,0 +1,15 @@
import Color from "../components/Color";
const COLORS = ["primary", "link", "info", "success", "warning", "danger"];
function Colors() {
return (
<div>
{COLORS.map((color) => {
return <Color key={color} color={color} />;
})}
</div>
);
}
export default Colors;

View File

@ -0,0 +1,16 @@
import Slider from "../components/Slider";
import { CSSVAR_KEYS } from "../constants";
function Scheme() {
const schemeIds = CSSVAR_KEYS.scheme;
return (
<div>
{schemeIds.map((schemeId) => {
return <Slider key={schemeId} id={schemeId} />;
})}
</div>
);
}
export default Scheme;

View File

@ -0,0 +1,9 @@
export function unslug(slug) {
// Replace hyphens and underscores with spaces
let result = slug.replace(/[-_]/g, " ");
// Capitalize the first letter of each word
return result.replace(/\b\w/g, function (char) {
return char.toUpperCase();
});
}

View File

@ -4,6 +4,7 @@ import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
build: { build: {
emptyOutDir: true,
outDir: "../../assets/javascript/bulma-customizer", outDir: "../../assets/javascript/bulma-customizer",
rollupOptions: { rollupOptions: {
output: { output: {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -3,14 +3,14 @@ layout: default
theme: customizer theme: customizer
route: customizer route: customizer
--- ---
{% include global/header.html %}
{% {% include global/header.html %} {% include docs/hero.html title="The Bulma
include docs/hero.html Customizer" subtitle="Gorgeous websites built with Bulma." %}
title="The Bulma Customizer"
subtitle="Gorgeous websites built with Bulma."
%}
<div id="root"></div> <div id="root"></div>
<link
rel="stylesheet"
href="{{ site.url }}/assets/javascript/bulma-customizer/index.css"
/>
<script src="{{ site.url }}/assets/javascript/bulma-customizer/index.js"></script> <script src="{{ site.url }}/assets/javascript/bulma-customizer/index.js"></script>