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

Button type? #501

Open
TheKnarf opened this issue May 15, 2024 · 2 comments
Open

Button type? #501

TheKnarf opened this issue May 15, 2024 · 2 comments

Comments

@TheKnarf
Copy link

TheKnarf commented May 15, 2024

I don't see an input type for button under inputs.

I think the syntax should be something like the following:

useControls({
  myButton1: () => { console.log("button mybutton1 was clicked") },
  myButton2: () => { console.log("button mybutton2 was clicked") },
});

Where useControls would detect if a parameter was typeof Function and then render it as a button that you can click on.

Seems like there is even an empty docs section mentioning buttons?

@TheKnarf
Copy link
Author

TheKnarf commented May 15, 2024

I tried creating a plugin for it:

import React from 'react';
import { Leva, useControls } from 'leva';
import { createPlugin } from 'leva/plugin';
import { useInputContext, Components } from 'leva/plugin';

const { Row, Label } = Components;

const LevaButton = (...args) => {
  const { label, displayValue, onUpdate, settings } = useInputContext<any>();
  
  const onClick = () => {
    if(typeof displayValue.onClick == 'function') {
      displayValue.onClick();
    }
  };
  
  return <>
    <Row input>
      <Label>{label}</Label>
      <button type="button" onClick={onClick}>{displayValue.name || label}</button>
    </Row>
  </>
};

export const button = createPlugin({
  component: LevaButton,
});

export { Leva, useControls };

And then I can use it as:

import { Leva, useControls, button } from '../leva/leva.tsx';

const MyComponent : React.FC = () => {
  useControls({
    btn1: button({
      name: 'Button 1',
      onClick: () => { console.log('button 1') },
    }),
    btn2: button({
      name: 'Button 2',
      onClick: () => { console.log('button 2') },
    }),
  });

  return <div>
    <Leva />
  </div>;
};

But the styling is a bit weird, and I'm unsure if I'm creating a plugin in the correct way given that its not documented. This also forces me to wrap each property in a button when using useControls. So I still think it would be useful to have it built inn.

@TheKnarf
Copy link
Author

Okay... So after writing my own plugin I found that leva already had a button component in their storybook...

It just wasn't documented in the docs folder...

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