Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[joy-ui][theme] Improve theme builder exceptions #38709

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions docs/src/modules/components/JoyThemeBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ export default theme;`;
function getPaletteFormProps(colorSchemes: any, colorMode: string, node: string) {
// @ts-ignore
const themeDefaultValue = defaultTheme.colorSchemes[colorMode].palette[node];
const value = colorSchemes[colorMode][node];
const value = colorSchemes[colorMode][node] || {};

const mergedValue = { ...themeDefaultValue, ...value };
return {
themeDefaultValue,
Expand Down Expand Up @@ -719,6 +720,7 @@ function PaletteImport({
function ColorTokenCreator({ onChange }: { onChange: (name: string, value: string) => void }) {
const [open, setOpen] = React.useState(false);
const nameRef = React.useRef<HTMLInputElement | null>(null);
const colorRef = React.useRef<HTMLInputElement | null>(null);
const [name, setName] = React.useState('');
const [color, setColor] = React.useState('');
if (!open) {
Expand All @@ -739,6 +741,9 @@ function ColorTokenCreator({ onChange }: { onChange: (name: string, value: strin
</Button>
);
}

const isValidToken = name.trim() && color.trim();

return (
<Sheet
variant="soft"
Expand All @@ -763,28 +768,41 @@ function ColorTokenCreator({ onChange }: { onChange: (name: string, value: strin
onChange={(event) => setName(event.target.value)}
/>{' '}
<b>:</b>{' '}
<Input
<ColorInput
size="sm"
placeholder="A valid CSS color"
value={color}
onChange={(event) => setColor(event.target.value)}
onEmptyColor={() => {
setColor('');
}}
onKeyDown={(event) => {
if (event.key === 'Enter' && name && color) {
onChange(name, color);
setOpen(false);
}
}}
onValidColor={(newColor) => {
setColor(newColor);
}}
slotProps={{
input: { ref: colorRef },
}}
sx={{ flexGrow: 1 }}
/>
<IconButton
variant="solid"
color="neutral"
color={isValidToken ? 'primary' : 'neutral'}
size="sm"
onClick={() => {
if (!name) {
const trimmedName = name.trim();
const trimmedColor = color.trim();

if (!trimmedName) {
nameRef.current?.focus();
} else if (!trimmedColor) {
colorRef.current?.focus();
} else {
onChange(name, color);
onChange(trimmedName, trimmedColor);
setColor('');
setName('');
setOpen(false);
Expand Down Expand Up @@ -1582,7 +1600,7 @@ export default function JoyThemeBuilder() {
}}
onRemove={(token) => {
setter((prev) => {
const newPalette = prev.background;
const newPalette = prev.background || {};
delete newPalette[token];
return { ...prev, background: newPalette };
});
Expand All @@ -1606,7 +1624,7 @@ export default function JoyThemeBuilder() {
}}
onRemove={(token) => {
setter((prev) => {
const newPalette = prev.common;
const newPalette = prev.common || {};
delete newPalette[token];
return { ...prev, common: newPalette };
});
Expand All @@ -1630,7 +1648,7 @@ export default function JoyThemeBuilder() {
}}
onRemove={(token) => {
setter((prev) => {
const newPalette = prev.text;
const newPalette = prev.text || {};
delete newPalette[token];
return { ...prev, text: newPalette };
});
Expand Down Expand Up @@ -1686,7 +1704,7 @@ export default function JoyThemeBuilder() {
}}
onRemove={(token) => {
setter((prev) => {
const newPalette = prev[colorProp];
const newPalette = prev[colorProp] || {};
delete newPalette[token];
return { ...prev, [colorProp]: newPalette };
});
Expand All @@ -1707,7 +1725,7 @@ export default function JoyThemeBuilder() {
}}
onRemove={(token) => {
setter((prev) => {
const newPalette = prev[colorProp];
const newPalette = prev[colorProp] || {};
delete newPalette[token];
return { ...prev, [colorProp]: newPalette };
});
Expand Down