-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: theme builder options config (#2955)
- Loading branch information
Showing
11 changed files
with
293 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from "react"; | ||
import clsx from "clsx"; | ||
import { FaChevronDown } from "react-icons/fa"; | ||
|
||
type AccordionProps = { | ||
id: string; | ||
title: string; | ||
children: React.ReactNode; | ||
defaultOpen?: boolean; | ||
}; | ||
|
||
const Accordion = ({ | ||
id, | ||
title, | ||
children, | ||
defaultOpen = false, | ||
}: AccordionProps) => { | ||
const [isOpen, setIsOpen] = React.useState(defaultOpen); | ||
|
||
const toggleAccordion = () => { | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
return ( | ||
<div id={id} className="group"> | ||
<h2 id={`${id}-heading`}> | ||
<button | ||
type="button" | ||
className={clsx( | ||
"flex items-center justify-between w-full px-5 py-3 text-sm font-bold rtl:text-right text-gray-500 border border-b-0 border-gray-200 gap-3 group-last:border-b", | ||
{ "group-last:border-b-0": isOpen }, | ||
)} | ||
aria-expanded="true" | ||
aria-controls={`${id}-body`} | ||
onClick={toggleAccordion} | ||
> | ||
<span>{title}</span> | ||
<FaChevronDown | ||
className={clsx("w-3 h-3 shrink-0", { "rotate-180 ": isOpen })} | ||
/> | ||
</button> | ||
</h2> | ||
<div | ||
id={`${id}-body`} | ||
className={isOpen ? "block" : "hidden"} | ||
aria-labelledby={`${id}-heading`} | ||
> | ||
<div className="p-5 border border-b-0 border-gray-200 group-last:border-b"> | ||
{children} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
export default Accordion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React from "react"; | ||
import optionsConfig from "./options-config"; | ||
import Accordion from "./accordion"; | ||
import Select from "./select"; | ||
import Slider from "./slider"; | ||
import ColorPicker from "./color-picker"; | ||
import ColorScaleOptions from "./color-scale-options"; | ||
import { getConfigValue } from "./utils"; | ||
|
||
const ConfigMapper = ({ | ||
themeConfig, | ||
activeColorScale, | ||
updateThemeConfig, | ||
handleColorScaleChange, | ||
}) => { | ||
const handleColorChange = ({ newColor, index, colorScale }) => { | ||
const updatedColors = themeConfig?.palette?.[colorScale]?.map((color, i) => | ||
i === index ? newColor : color, | ||
); | ||
updateThemeConfig(`palette.${colorScale}`, updatedColors); | ||
}; | ||
|
||
return ( | ||
<> | ||
{optionsConfig.map((section, index) => ( | ||
<Accordion | ||
key={section.title} | ||
title={section.title} | ||
id={section.title} | ||
defaultOpen={index === 0} | ||
> | ||
{section.fields.map((field) => { | ||
if (field.type === "colorScale") { | ||
return ( | ||
<ColorScaleOptions | ||
key={field.label} | ||
activeColorScale={activeColorScale} | ||
palette={themeConfig?.palette} | ||
onColorChange={handleColorChange} | ||
onColorScaleChange={handleColorScaleChange} | ||
/> | ||
); | ||
} | ||
const configValue = getConfigValue(themeConfig, field.path); | ||
if (field.type === "slider") { | ||
return ( | ||
<Slider | ||
id={field.label} | ||
key={field.label} | ||
label={field.label} | ||
value={configValue as number} | ||
unit={field.unit} | ||
onChange={(newValue) => | ||
updateThemeConfig(field.path, newValue) | ||
} | ||
/> | ||
); | ||
} | ||
if (field.type === "select") { | ||
return ( | ||
<Select | ||
id={field.label} | ||
key={field.label} | ||
label={field.label} | ||
value={configValue as string} | ||
options={field.options} | ||
onChange={(newValue) => | ||
updateThemeConfig(field.path, newValue) | ||
} | ||
/> | ||
); | ||
} | ||
if (field.type === "colorPicker") { | ||
return ( | ||
<ColorPicker | ||
id={field.label} | ||
key={field.label} | ||
label={field.label} | ||
color={configValue as string} | ||
onColorChange={(newColor) => | ||
updateThemeConfig(field.path, newColor) | ||
} | ||
showColorName | ||
/> | ||
); | ||
} | ||
return null; | ||
})} | ||
</Accordion> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default ConfigMapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.