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

#382: Refactor ThemePanel component to use useControllableState hook #609

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 29 additions & 24 deletions packages/radix-ui-themes/src/components/theme-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as React from 'react';
import { useCallbackRef } from '@radix-ui/react-use-callback-ref';
import { useControllableState } from '@radix-ui/react-use-controllable-state';
import {
AccessibleIcon,
Box,
Expand All @@ -23,30 +24,28 @@ import { themePropDefs } from '../props/index.js';
import type { ComponentPropsWithout, RemovedProps } from '../helpers/index.js';
import type { GetPropDefTypes } from '../props/index.js';

interface ThemePanelProps extends Omit<ThemePanelImplProps, keyof ThemePanelImplPrivateProps> {
defaultOpen?: boolean;
}
const ThemePanel = React.forwardRef<ThemePanelImplElement, ThemePanelProps>(
({ defaultOpen = true, ...props }, forwardedRef) => {
const [open, setOpen] = React.useState(defaultOpen);
return <ThemePanelImpl {...props} ref={forwardedRef} open={open} onOpenChange={setOpen} />;
}
);
ThemePanel.displayName = 'ThemePanel';

type ThemePanelImplElement = React.ElementRef<'div'>;
interface ThemePanelImplPrivateProps {
open: boolean;
onOpenChange: (open: boolean) => void;
}
interface ThemePanelImplProps
extends ComponentPropsWithout<'div', RemovedProps>,
ThemePanelImplPrivateProps {

interface ThemePanelProps extends ComponentPropsWithout<'div', RemovedProps> {
open?: boolean;
onOpenChange?: (open: boolean) => void;
/** Whether the theme panel is open by default.
* Doesn't have any effect if `open` is also set.
* @default true
*/
defaultOpen?: boolean;
onAppearanceChange?: (value: 'light' | 'dark') => void;
}
const ThemePanelImpl = React.forwardRef<ThemePanelImplElement, ThemePanelImplProps>(

const ThemePanel = React.forwardRef<ThemePanelImplElement, ThemePanelProps>(
(props, forwardedRef) => {
const { open, onOpenChange, onAppearanceChange: onAppearanceChangeProp, ...panelProps } = props;
const {
open: openProp,
defaultOpen,
onOpenChange,
onAppearanceChange: onAppearanceChangeProp,
...panelProps
} = props;
const themeContext = useThemeContext();
const {
appearance,
Expand All @@ -63,6 +62,12 @@ const ThemePanelImpl = React.forwardRef<ThemePanelImplElement, ThemePanelImplPro
onScalingChange,
} = themeContext;

const [open = true, setOpen] = useControllableState({
prop: openProp,
defaultProp: defaultOpen,
onChange: onOpenChange,
});

const hasOnAppearanceChangeProp = onAppearanceChangeProp !== undefined;
const handleAppearanceChangeProp = useCallbackRef(onAppearanceChangeProp);
const handleAppearanceChange = React.useCallback(
Expand Down Expand Up @@ -135,12 +140,12 @@ const ThemePanelImpl = React.forwardRef<ThemePanelImplElement, ThemePanelImplPro
const isKeyboardInputActive = document.activeElement?.closest(keyboardInputElement);
const isKeyT = event.key?.toUpperCase() === 'T' && !isModifierActive;
if (isKeyT && !isKeyboardInputActive) {
onOpenChange(!open);
setOpen((prev) => !prev);
}
}
document.addEventListener('keydown', handleKeydown);
return () => document.removeEventListener('keydown', handleKeydown);
}, [onOpenChange, open, keyboardInputElement]);
}, [setOpen, keyboardInputElement]);

// quickly toggle appearance using "D" keypress
React.useEffect(() => {
Expand Down Expand Up @@ -230,7 +235,7 @@ const ThemePanelImpl = React.forwardRef<ThemePanelImplElement, ThemePanelImplPro
sideOffset={6}
>
<Kbd asChild size="3" tabIndex={0} className="rt-ThemePanelShortcut">
<button onClick={() => onOpenChange(!open)}>T</button>
<button onClick={() => setOpen((prev) => !prev)}>T</button>
</Kbd>
</Tooltip>
</Box>
Expand Down Expand Up @@ -621,7 +626,7 @@ const ThemePanelImpl = React.forwardRef<ThemePanelImplElement, ThemePanelImplPro
);
}
);
ThemePanelImpl.displayName = 'ThemePanelImpl';
ThemePanel.displayName = 'ThemePanel';

// https://github.com/pacocoursey/next-themes/blob/main/packages/next-themes/src/index.tsx#L285
function disableAnimation() {
Expand Down