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

155 lines
3.5 KiB
React
Raw Normal View History

2024-06-25 03:59:13 +01:00
import { createContext, useEffect, useState } from "react";
2024-06-24 01:08:40 +01:00
import "../../../../css/bulma.css";
2024-06-25 03:59:13 +01:00
import cn from "./App.module.css";
import Color from "./components/Color";
2024-06-24 01:08:40 +01:00
2024-06-24 04:10:08 +01:00
const COLORS = ["primary", "link", "info", "success", "warning", "danger"];
2024-06-24 03:25:58 +01:00
const KEYS = [
"scheme-h",
"primary-h",
"primary-s",
"primary-l",
2024-06-24 04:10:08 +01:00
"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",
2024-06-24 03:25:58 +01:00
"skeleton-lines-gap",
];
2024-06-24 01:08:40 +01:00
const UNITS = ["deg", "rem", "em", "%"];
2024-06-24 03:25:58 +01:00
const SUFFIX_TO_KIND = {
"-h": "hue",
"-s": "saturation",
"-l": "lightness",
"-gap": "gap",
};
2024-06-24 01:08:40 +01:00
2024-06-25 03:59:13 +01:00
export const CustomizerContext = createContext({
cssvars: {},
getVar: () => {},
updateVar: () => {},
});
2024-06-24 01:08:40 +01:00
function App() {
2024-06-25 03:59:13 +01:00
const initialContext = {
cssvars: {},
getVar: (id) => {
return context.cssvars[id];
},
2024-06-25 16:20:07 +01:00
updateVar: (id, newValue) => {
setContext((context) => {
const { start, unit } = context.cssvars[id];
const computedValue = `${newValue}${unit}`;
2024-06-25 13:48:53 +01:00
2024-06-25 16:20:07 +01:00
if (start === newValue) {
document.documentElement.style.removeProperty(`--bulma-${id}`);
} else {
document.documentElement.style.setProperty(
`--bulma-${id}`,
computedValue,
);
}
2024-06-25 13:48:53 +01:00
2024-06-25 03:59:13 +01:00
return {
...context,
cssvars: {
...context.cssvars,
[id]: {
...context.cssvars[id],
2024-06-25 13:48:53 +01:00
current: newValue,
2024-06-25 03:59:13 +01:00
},
},
};
});
},
};
const [context, setContext] = useState(initialContext);
2024-06-24 01:08:40 +01:00
useEffect(() => {
const rootStyle = window.getComputedStyle(document.documentElement);
2024-06-24 04:10:08 +01:00
const cssvars = {};
KEYS.map((key) => {
2024-06-24 01:08:40 +01:00
const original = rootStyle.getPropertyValue(`--bulma-${key}`);
2024-06-24 03:25:58 +01:00
const suffix = Object.keys(SUFFIX_TO_KIND).find((kind) =>
key.endsWith(kind),
);
2024-06-24 01:08:40 +01:00
const unit = UNITS.find((unit) => original.endsWith(unit)) || "";
const value = unit !== "" ? original.split(unit)[0] : original;
2024-06-24 04:10:08 +01:00
cssvars[key] = {
2024-06-24 01:08:40 +01:00
id: key,
2024-06-24 03:25:58 +01:00
kind: SUFFIX_TO_KIND[suffix] || "any",
2024-06-24 01:08:40 +01:00
original,
unit,
2024-06-25 03:59:13 +01:00
current: Number(value),
2024-06-24 01:08:40 +01:00
start: Number(value),
};
});
2024-06-25 03:59:13 +01:00
setContext((context) => {
return {
...context,
cssvars,
};
});
2024-06-24 01:08:40 +01:00
}, []);
2024-06-25 03:59:13 +01:00
// 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]);
2024-06-24 01:08:40 +01:00
return (
2024-06-25 03:59:13 +01:00
<CustomizerContext.Provider value={context}>
<section className="section">
<div className="card">
<div className="card-content">
<div className={cn.colors}>
{COLORS.map((color) => {
return <Color key={color} color={color} />;
})}
</div>
2024-06-24 01:08:40 +01:00
</div>
</div>
2024-06-25 03:59:13 +01:00
</section>
</CustomizerContext.Provider>
2024-06-24 01:08:40 +01:00
);
}
export default App;