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

235 lines
6.5 KiB
React
Raw Normal View History

2024-06-25 03:59:13 +01:00
import { createContext, useEffect, useState } from "react";
2024-06-25 22:20:37 +01:00
import classNames from "classnames";
2024-06-25 23:28:12 +01:00
import "../../../../css/bulma.css";
2024-06-25 22:20:37 +01:00
2024-06-26 00:49:42 +01:00
import { CSSVAR_KEYS } from "./constants";
2024-06-25 22:20:37 +01:00
import { unslug } from "./utils";
2024-06-25 03:59:13 +01:00
2024-06-25 23:28:12 +01:00
import Colors from "./pages/Colors";
2024-06-25 22:20:37 +01:00
import Scheme from "./pages/Scheme";
2024-06-25 23:28:12 +01:00
import Typography from "./pages/Typography";
2024-06-26 00:49:42 +01:00
import Other from "./pages/Other";
import Generic from "./pages/Generic";
2024-06-26 01:49:55 +01:00
import Skeleton from "./pages/Skeleton";
2024-06-26 02:36:41 +01:00
import Box from "./pages/elements/Box";
import Content from "./pages/elements/Content";
import Delete from "./pages/elements/Delete";
import Icon from "./pages/elements/Icon";
import Notification from "./pages/elements/Notification";
import Progress from "./pages/elements/Progress";
import Table from "./pages/elements/Table";
import Tag from "./pages/elements/Tag";
import Title from "./pages/elements/Title";
2024-06-26 00:49:42 +01:00
const SUFFIX_TO_KIND = {
"-h": "hue",
"-s": "saturation",
"-l": "lightness",
"-delta": "delta",
"-color": "color",
};
const UNITS = ["deg", "%"];
2024-06-25 22:20:37 +01:00
const PAGE_TO_COMPONENT = {
colors: <Colors />,
scheme: <Scheme />,
2024-06-25 23:28:12 +01:00
typography: <Typography />,
2024-06-26 00:49:42 +01:00
other: <Other />,
generic: <Generic />,
2024-06-26 01:49:55 +01:00
skeleton: <Skeleton />,
box: <Box />,
2024-06-26 02:36:41 +01:00
content: <Content />,
delete: <Delete />,
icon: <Icon />,
notification: <Notification />,
progress: <Progress />,
table: <Table />,
tag: <Tag />,
title: <Title />,
2024-06-24 03:25:58 +01:00
};
2024-06-26 01:49:55 +01:00
const PAGE_IDS = [
"colors",
"scheme",
"typography",
"other",
"generic",
"skeleton",
"box",
2024-06-26 02:36:41 +01:00
"content",
"delete",
"icon",
"notification",
"progress",
"table",
"tag",
"title",
2024-06-26 01:49:55 +01:00
];
2024-06-24 01:08:40 +01:00
2024-06-25 03:59:13 +01:00
export const CustomizerContext = createContext({
cssvars: {},
2024-06-25 22:20:37 +01:00
currentPage: "",
2024-06-25 03:59:13 +01:00
getVar: () => {},
2024-06-25 22:20:37 +01:00
changeTab: () => {},
2024-06-25 03:59:13 +01:00
updateVar: () => {},
});
2024-06-24 01:08:40 +01:00
function App() {
2024-06-25 03:59:13 +01:00
const initialContext = {
cssvars: {},
2024-06-26 02:36:41 +01:00
currentPage: "delete",
2024-06-25 03:59:13 +01:00
getVar: (id) => {
return context.cssvars[id];
},
2024-06-25 22:20:37 +01:00
changeTab: (pageId) => {
setContext((context) => {
return {
...context,
currentPage: pageId,
};
});
},
2024-06-25 16:20:07 +01:00
updateVar: (id, newValue) => {
setContext((context) => {
2024-06-26 01:49:55 +01:00
const { start, unit, scope } = context.cssvars[id];
2024-06-25 16:20:07 +01:00
const computedValue = `${newValue}${unit}`;
2024-06-26 01:49:55 +01:00
const el = document.querySelector(scope);
2024-06-25 13:48:53 +01:00
2024-06-25 16:20:07 +01:00
if (start === newValue) {
2024-06-26 01:49:55 +01:00
el.style.removeProperty(`--bulma-${id}`);
2024-06-25 16:20:07 +01:00
} else {
2024-06-26 01:49:55 +01:00
el.style.setProperty(`--bulma-${id}`, computedValue);
2024-06-25 16:20:07 +01:00
}
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-25 22:20:37 +01:00
const handleTabChange = (event, pageId) => {
event.preventDefault();
context.changeTab(pageId);
};
2024-06-24 01:08:40 +01:00
useEffect(() => {
2024-06-26 01:49:55 +01:00
const styles = {
root: window.getComputedStyle(document.documentElement),
box: window.getComputedStyle(document.querySelector(".box")),
2024-06-26 02:36:41 +01:00
content: window.getComputedStyle(document.querySelector(".content")),
delete: window.getComputedStyle(document.querySelector(".delete")),
icon: window.getComputedStyle(document.querySelector(".icon")),
notification: window.getComputedStyle(
document.querySelector(".notification"),
),
progress: window.getComputedStyle(document.querySelector(".progress")),
table: window.getComputedStyle(document.querySelector(".table")),
tag: window.getComputedStyle(document.querySelector(".tag")),
title: window.getComputedStyle(document.querySelector(".title")),
2024-06-26 01:49:55 +01:00
};
2024-06-24 01:08:40 +01:00
2024-06-24 04:10:08 +01:00
const cssvars = {};
2024-06-25 22:20:37 +01:00
const allKeys = PAGE_IDS.map((pageId) => CSSVAR_KEYS[pageId]).flat();
2024-06-25 23:12:07 +01:00
const allKeyIds = allKeys.map((i) => i.id);
2024-06-24 04:10:08 +01:00
2024-06-25 23:12:07 +01:00
allKeyIds.map((key) => {
2024-06-26 01:49:55 +01:00
let original;
let scope = ":root";
if (key.startsWith("box")) {
scope = ".box";
original = styles.box.getPropertyValue(`--bulma-${key}`);
2024-06-26 02:36:41 +01:00
} else if (key.startsWith("content")) {
scope = ".content";
original = styles.content.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("delete")) {
scope = ".delete";
original = styles.delete.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("icon")) {
scope = ".icon";
original = styles.icon.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("notification")) {
scope = ".notification";
original = styles.notification.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("progress")) {
scope = ".progress";
original = styles.progress.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("table")) {
scope = ".table";
original = styles.table.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("tag")) {
scope = ".tag";
original = styles.tag.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("title")) {
scope = ".title";
original = styles.title.getPropertyValue(`--bulma-${key}`);
2024-06-26 01:49:55 +01:00
} else {
original = styles.root.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-25 23:12:07 +01:00
const description =
allKeys.find((el) => el.id === key)?.description || "None";
2024-06-24 01:08:40 +01:00
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 23:28:12 +01:00
current: value,
start: value,
2024-06-25 23:12:07 +01:00
description,
2024-06-26 01:49:55 +01:00
scope,
2024-06-24 01:08:40 +01:00
};
});
2024-06-25 03:59:13 +01:00
setContext((context) => {
return {
...context,
cssvars,
};
});
2024-06-24 01:08:40 +01:00
}, []);
return (
2024-06-25 03:59:13 +01:00
<CustomizerContext.Provider value={context}>
<section className="section">
2024-06-25 22:20:37 +01:00
<div className="buttons">
{PAGE_IDS.map((pageId) => {
const buttonClass = classNames({
button: true,
2024-06-25 23:28:12 +01:00
"is-link": pageId === context.currentPage,
2024-06-25 22:20:37 +01:00
});
return (
<button
className={buttonClass}
key={pageId}
onClick={(e) => handleTabChange(e, pageId)}
>
{unslug(pageId)}
</button>
);
})}
2024-06-24 01:08:40 +01:00
</div>
2024-06-25 22:20:37 +01:00
{PAGE_TO_COMPONENT[context.currentPage]}
2024-06-25 03:59:13 +01:00
</section>
</CustomizerContext.Provider>
2024-06-24 01:08:40 +01:00
);
}
export default App;