npm i leva
To use Leva, simply import useControls
and use it anywhere in your app:
import { useControls } from 'leva'
function MyComponent() {
const { myValue } = useControls({ myValue: 10 })
return myValue
}
function AnotherComponent() {
const { anotherValue } = useControls({ anotherValue: 'alive!!' })
return <div>Hey, I'm {anotherValue}</div>
}
function UnmountedComponent() {
const { barValue } = useControls({ barValue: false })
return barValue ? <div>Hello!</div> : null
}
function MyApp() {
return (
<>
<MyComponent />
<AnotherComponent />
</>
)
}
Note that since UnmountedComponent
is not mounted anywhere in our application, the control declared there will not be added to the GUI!
💡 The order of the controls depends on hooks call order, in our case myValue
will be first.
Leva will automagically use the best input type for your values, all the rules can be found in the Inputs page
useControls({
check: false,
myNumber: 4,
color: { value: '#ffffffff', render: (get) => get('check') && get('myNumber') > 5 },
})
The color
input will show in the pane only if check
is true
and myNumber
is strictly greater than 5
.
Using a folder structure can be as easy as setting the name you want as the first parameter of useControls
.
useControls('My folder', {
showLighting: true,
showStats: false,
})
Say you want folders in your folders. For this we are going to need the folder
function. Using this method the object key becomes the folder name
import { folder, useControls } from 'leva'
const { showLighting, showStats } = useControls('My folder', {
lighting: folder({
showLighting: true,
}),
'Show stats': folder({
showStats: false,
}),
})
{
showLighting: true,
showStats: false
}
Notice how they are at the top level and the folder properties are ignored. This means that having properties with the same names in different folders will cause conflicts.