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

Helper components? LevaSwitch #489

Open
bhouston opened this issue Jan 31, 2024 · 0 comments
Open

Helper components? LevaSwitch #489

bhouston opened this issue Jan 31, 2024 · 0 comments

Comments

@bhouston
Copy link

In my project, I have the need to create easy toggleable elements. To do this, I created this component:

import { useControls } from 'leva';

export type LevaSwitchProps = {
  name: string;
  initialState?: boolean;
  children: React.ReactNode;
};

export const LevaSwitch: React.FC<LevaSwitchProps> = ({
  name,
  initialState,
  children
}) => {
  const controls = useControls({
    [name]: {
      value: initialState === false ? false : true,
      label: name
    }
  });
  return controls[name] ? children : null;
};

Then you can use it in your code incredibly simply:

<LevaSwitch name="debugLog" initialState="true"><DebugLog></LevaSwitch>

This makes it very easy to make toggle-able elements. Maybe this type of component could be included in Leva? I have some ideas for other helper components as well..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant