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

497 lines
14 KiB
React
Raw Normal View History

2024-06-26 16:06:52 +01:00
import { createContext, useEffect, useRef, 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-27 02:00:43 +01:00
import "./App.css";
2024-06-26 20:00:17 +01:00
import cn from "./App.module.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
2024-06-26 20:00:17 +01:00
import Breadcrumb from "./pages/components/Breadcrumb";
import Card from "./pages/components/Card";
import Dropdown from "./pages/components/Dropdown";
import Menu from "./pages/components/Menu";
import Message from "./pages/components/Message";
import Modal from "./pages/components/Modal";
import Navbar from "./pages/components/Navbar";
import Pagination from "./pages/components/Pagination";
import Panel from "./pages/components/Panel";
import Tabs from "./pages/components/Tabs";
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 16:06:52 +01:00
import Control from "./pages/form/Control";
import Input from "./pages/form/Input";
import File from "./pages/form/File";
import Columns from "./pages/grid/Columns";
import Grid from "./pages/grid/Grid";
import Footer from "./pages/layout/Footer";
import Hero from "./pages/layout/Hero";
import Media from "./pages/layout/Media";
import Section from "./pages/layout/Section";
2024-06-27 02:00:43 +01:00
import Export from "./components/Export";
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 />,
2024-06-26 20:00:17 +01:00
// Components
breadcrumb: <Breadcrumb />,
card: <Card />,
dropdown: <Dropdown />,
menu: <Menu />,
message: <Message />,
modal: <Modal />,
navbar: <Navbar />,
pagination: <Pagination />,
panel: <Panel />,
tabs: <Tabs />,
2024-06-26 16:06:52 +01:00
// Elements
2024-06-26 01:49:55 +01:00
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-26 16:06:52 +01:00
// Form
control: <Control />,
input: <Input />,
file: <File />,
// Grid
columns: <Columns />,
grid: <Grid />,
// Layout
footer: <Footer />,
hero: <Hero />,
media: <Media />,
section: <Section />,
2024-06-27 02:00:43 +01:00
// Export
export: <Export />,
2024-06-24 03:25:58 +01:00
};
2024-06-26 20:00:17 +01:00
const TAB_IDS = [
"Global Variables",
"Components",
"Elements",
"Form",
"Grid",
"Layout",
2024-06-27 02:00:43 +01:00
"Export",
2024-06-26 01:49:55 +01:00
];
2024-06-26 20:00:17 +01:00
const PAGE_IDS = {
"Global Variables": [
"colors",
"scheme",
"typography",
"other",
"generic",
"skeleton",
],
Components: [
"breadcrumb",
"card",
"dropdown",
"menu",
"message",
"modal",
"navbar",
"pagination",
"panel",
"tabs",
],
Elements: [
"box",
"content",
"delete",
"icon",
"notification",
"progress",
"table",
"tag",
"title",
],
Form: ["control", "input", "file"],
Grid: ["columns", "grid"],
Layout: ["footer", "hero", "media", "section"],
2024-06-27 02:00:43 +01:00
Export: ["export"],
2024-06-26 20:00:17 +01:00
};
2024-06-24 01:08:40 +01:00
2024-06-25 03:59:13 +01:00
export const CustomizerContext = createContext({
2024-06-27 01:03:51 +01:00
isOpen: false,
2024-06-25 03:59:13 +01:00
cssvars: {},
2024-06-26 20:00:17 +01:00
currentTab: "",
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-26 20:00:17 +01:00
changePage: () => {},
2024-06-25 03:59:13 +01:00
updateVar: () => {},
});
2024-06-24 01:08:40 +01:00
function App() {
2024-06-26 16:06:52 +01:00
const styleRef = useRef();
2024-06-25 03:59:13 +01:00
const initialContext = {
2024-06-27 01:03:51 +01:00
isOpen: true,
2024-06-25 03:59:13 +01:00
cssvars: {},
2024-06-27 02:00:43 +01:00
currentTab: "Export",
currentPage: "export",
2024-06-25 03:59:13 +01:00
getVar: (id) => {
return context.cssvars[id];
},
2024-06-26 20:00:17 +01:00
changeTab: (tabId) => {
setContext((context) => {
return {
...context,
currentTab: tabId,
};
});
},
changePage: (pageId) => {
2024-06-25 22:20:37 +01:00
setContext((context) => {
return {
...context,
currentPage: pageId,
};
});
},
2024-06-25 16:20:07 +01:00
updateVar: (id, newValue) => {
setContext((context) => {
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-27 00:35:13 +01:00
const handleTabChange = (event) => {
2024-06-25 22:20:37 +01:00
event.preventDefault();
2024-06-27 00:35:13 +01:00
const tabId = event.target.value;
2024-06-26 20:00:17 +01:00
context.changeTab(tabId);
context.changePage(PAGE_IDS[tabId][0]);
};
const handlePageChange = (event, pageId) => {
event.preventDefault();
context.changePage(pageId);
2024-06-25 22:20:37 +01:00
};
2024-06-27 01:03:51 +01:00
const handleOpening = (event) => {
event.preventDefault();
setContext((context) => {
return {
...context,
isOpen: !context.isOpen,
};
});
};
2024-06-24 01:08:40 +01:00
useEffect(() => {
2024-06-26 01:49:55 +01:00
const styles = {
root: window.getComputedStyle(document.documentElement),
2024-06-26 20:00:17 +01:00
breadcrumb: window.getComputedStyle(
document.querySelector(".breadcrumb"),
),
card: window.getComputedStyle(document.querySelector(".card")),
dropdown: window.getComputedStyle(document.querySelector(".dropdown")),
menu: window.getComputedStyle(document.querySelector(".menu")),
message: window.getComputedStyle(document.querySelector(".message")),
modal: window.getComputedStyle(document.querySelector(".modal")),
navbar: window.getComputedStyle(document.querySelector(".navbar")),
pagination: window.getComputedStyle(
document.querySelector(".pagination"),
),
panel: window.getComputedStyle(document.querySelector(".panel")),
tabs: window.getComputedStyle(document.querySelector(".tabs")),
2024-06-26 01:49:55 +01:00
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 16:06:52 +01:00
file: window.getComputedStyle(document.querySelector(".file")),
input: window.getComputedStyle(document.querySelector(".input")),
columns: window.getComputedStyle(document.querySelector(".columns")),
grid: window.getComputedStyle(document.querySelector(".grid")),
footer: window.getComputedStyle(document.querySelector(".footer")),
hero: window.getComputedStyle(document.querySelector(".hero")),
media: window.getComputedStyle(document.querySelector(".media")),
section: window.getComputedStyle(document.querySelector(".section")),
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-26 20:00:17 +01:00
const allKeys = Object.values(PAGE_IDS)
.flat()
2024-06-27 02:00:43 +01:00
.map((pageId) => {
if (!(pageId in CSSVAR_KEYS)) {
return;
}
return CSSVAR_KEYS[pageId];
})
2024-06-26 20:00:17 +01:00
.flat();
2024-06-27 02:00:43 +01:00
const allKeyIds = allKeys.map((i) => {
if (!i) {
return;
}
return i.id;
});
2024-06-24 04:10:08 +01:00
2024-06-25 23:12:07 +01:00
allKeyIds.map((key) => {
2024-06-27 02:00:43 +01:00
if (!key) {
return;
}
2024-06-26 01:49:55 +01:00
let original;
let scope = ":root";
2024-06-26 20:00:17 +01:00
if (key.startsWith("breadcrumb")) {
scope = ".breadcrumb";
original = styles.breadcrumb.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("card")) {
scope = ".card";
original = styles.card.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("dropdown")) {
scope = ".dropdown";
original = styles.dropdown.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("menu")) {
scope = ".menu";
original = styles.menu.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("message")) {
scope = ".message";
original = styles.message.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("modal")) {
scope = ".modal";
original = styles.modal.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("navbar")) {
scope = ".navbar";
original = styles.navbar.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("pagination")) {
scope = ".pagination";
original = styles.pagination.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("panel")) {
scope = ".panel";
original = styles.panel.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("tabs")) {
scope = ".tabs";
original = styles.tabs.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("box")) {
2024-06-26 01:49:55 +01:00
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 16:06:52 +01:00
} else if (key.startsWith("file")) {
scope = ".file";
original = styles.file.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("input")) {
scope = ".input";
original = styles.input.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("columns")) {
scope = ".columns";
original = styles.columns.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("grid")) {
scope = ".grid";
original = styles.grid.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("footer")) {
scope = ".footer";
original = styles.footer.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("hero")) {
scope = ".hero";
original = styles.hero.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("media")) {
scope = ".media";
original = styles.media.getPropertyValue(`--bulma-${key}`);
} else if (key.startsWith("section")) {
scope = ".section";
original = styles.section.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
}, []);
2024-06-26 16:06:52 +01:00
useEffect(() => {
const rules = {};
Object.values(context.cssvars).forEach((cssvar) => {
const { id, current, start, scope, unit } = cssvar;
if (current == start) {
return;
}
const computedValue = `${current}${unit}`;
2024-06-27 00:35:13 +01:00
const declaration = `--bulma-${id}: ${computedValue} !important;`;
2024-06-26 16:06:52 +01:00
if (scope in rules) {
rules[scope].push(declaration);
} else {
rules[scope] = [declaration];
}
});
let content = "";
for (const [key, arr] of Object.entries(rules)) {
content += `${key} {`;
arr.forEach((item) => (content += item));
content += `}`;
}
if (styleRef) {
styleRef.current.innerHTML = content;
}
}, [context.cssvars]);
2024-06-26 20:00:17 +01:00
const tabsStyle = {
"--bulma-tabs-link-active-color": "var(--bulma-primary)",
};
2024-06-27 01:26:39 +01:00
const customizerClass = classNames({
[cn.customizer]: true,
2024-06-27 01:03:51 +01:00
[cn.open]: context.isOpen,
});
const buttonClass = classNames({
[cn.button]: true,
"button is-primary": true,
});
2024-06-24 01:08:40 +01:00
return (
2024-06-25 03:59:13 +01:00
<CustomizerContext.Provider value={context}>
2024-06-27 01:26:39 +01:00
<div className={cn.app}>
<style ref={styleRef} />
<div className={customizerClass}>
<div className={cn.controls}>
<div className="select" style={tabsStyle}>
<select onChange={handleTabChange} value={context.currentTab}>
{TAB_IDS.map((tabId) => {
return (
<option key={tabId} value={tabId}>
{unslug(tabId)}
</option>
);
})}
</select>
</div>
{PAGE_IDS[context.currentTab].map((pageId) => {
const buttonClass = classNames({
button: true,
"is-primary": pageId === context.currentPage,
});
return (
<button
className={buttonClass}
key={pageId}
onClick={(e) => handlePageChange(e, pageId)}
>
{unslug(pageId)}
</button>
);
})}
2024-06-27 00:35:13 +01:00
</div>
2024-06-26 20:00:17 +01:00
2024-06-27 01:26:39 +01:00
{PAGE_TO_COMPONENT[context.currentPage]}
2024-06-24 01:08:40 +01:00
</div>
2024-06-25 22:20:37 +01:00
2024-06-27 01:26:39 +01:00
<button className={buttonClass} onClick={handleOpening}>
{context.isOpen ? "Close Customizer" : "Open Customizer"}
</button>
2024-06-26 20:00:17 +01:00
</div>
2024-06-25 03:59:13 +01:00
</CustomizerContext.Provider>
2024-06-24 01:08:40 +01:00
);
}
export default App;