|
| 1 | +/** |
| 2 | + * Example: Using Leva in Headless Mode |
| 3 | + * |
| 4 | + * This demonstrates how to use Leva's state management without |
| 5 | + * rendering the default HTML UI panel. |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { useState } from 'react' |
| 9 | +import { useControls as useControlsHeaded } from 'leva' |
| 10 | +import { useControls, useLevaInputs, useLevaInput } from 'leva/headless' |
| 11 | + |
| 12 | +export default function HeadlessDemo() { |
| 13 | + const [isHeadless, setIsHeadless] = useState(true) |
| 14 | + |
| 15 | + return ( |
| 16 | + <div style={{ padding: 20, fontFamily: 'monospace' }}> |
| 17 | + <h2>Leva Headless Demo</h2> |
| 18 | + |
| 19 | + <div style={{ marginBottom: 20 }}> |
| 20 | + <button |
| 21 | + onClick={() => setIsHeadless(!isHeadless)} |
| 22 | + style={{ |
| 23 | + padding: '10px 20px', |
| 24 | + fontSize: '16px', |
| 25 | + marginBottom: '20px', |
| 26 | + backgroundColor: isHeadless ? '#ff0055' : '#00ff55', |
| 27 | + color: 'white', |
| 28 | + border: 'none', |
| 29 | + borderRadius: '4px', |
| 30 | + cursor: 'pointer', |
| 31 | + }}> |
| 32 | + Switch to {isHeadless ? 'Headed' : 'Headless'} Mode |
| 33 | + </button> |
| 34 | + <p> |
| 35 | + <strong>Current Mode:</strong> {isHeadless ? 'Headless' : 'Headed'} |
| 36 | + </p> |
| 37 | + {!isHeadless && ( |
| 38 | + <p style={{ color: '#666', fontSize: '14px' }}> |
| 39 | + <strong>Note:</strong> In Headed mode, the Leva panel should be visible (top-right). The custom UI below |
| 40 | + should disappear. If the panel persists when switching to headless mode, there's a bug. |
| 41 | + </p> |
| 42 | + )} |
| 43 | + </div> |
| 44 | + |
| 45 | + {isHeadless ? <HeadlessComponent /> : <HeadedComponent />} |
| 46 | + </div> |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +function HeadlessComponent() { |
| 51 | + const values = useControls({ |
| 52 | + name: 'World', |
| 53 | + count: { value: 0, min: 0, max: 10, step: 1 }, |
| 54 | + speed: { value: 1, min: 0.1, max: 5, step: 0.1 }, |
| 55 | + color: '#ff0055', |
| 56 | + enabled: true, |
| 57 | + }) |
| 58 | + |
| 59 | + const inputs = useLevaInputs() |
| 60 | + |
| 61 | + return ( |
| 62 | + <> |
| 63 | + <div style={{ marginBottom: 40 }}> |
| 64 | + <h3>Current Values:</h3> |
| 65 | + <pre style={{ background: '#f0f0f0', padding: 10, borderRadius: 4 }}>{JSON.stringify(values, null, 2)}</pre> |
| 66 | + </div> |
| 67 | + |
| 68 | + <div> |
| 69 | + <h3>Custom UI Built from Metadata:</h3> |
| 70 | + <div style={{ display: 'flex', flexDirection: 'column', gap: 15 }}> |
| 71 | + {inputs.map(({ path }) => ( |
| 72 | + <CustomControl key={path} path={path} /> |
| 73 | + ))} |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + </> |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +function HeadedComponent() { |
| 81 | + const values = useControlsHeaded({ |
| 82 | + name: 'World', |
| 83 | + count: { value: 0, min: 0, max: 10, step: 1 }, |
| 84 | + speed: { value: 1, min: 0.1, max: 5, step: 0.1 }, |
| 85 | + color: '#ff0055', |
| 86 | + enabled: true, |
| 87 | + }) |
| 88 | + |
| 89 | + return ( |
| 90 | + <div style={{ marginBottom: 40 }}> |
| 91 | + <h3>Current Values:</h3> |
| 92 | + <pre style={{ background: '#f0f0f0', padding: 10, borderRadius: 4 }}>{JSON.stringify(values, null, 2)}</pre> |
| 93 | + <p style={{ marginTop: 20, color: '#666', fontSize: '14px' }}> |
| 94 | + The Leva panel should be visible in the top-right corner. When you switch back to headless mode, this panel |
| 95 | + should disappear completely. |
| 96 | + </p> |
| 97 | + </div> |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +// Custom control component that uses the headless input data |
| 102 | +function CustomControl({ path }: { path: string }) { |
| 103 | + const levaInput = useLevaInput(path) |
| 104 | + |
| 105 | + if (!levaInput) return null |
| 106 | + |
| 107 | + const { input, set } = levaInput |
| 108 | + |
| 109 | + // Handle different input types with custom UI |
| 110 | + if ('value' in input) { |
| 111 | + const dataInput = input as any |
| 112 | + |
| 113 | + return ( |
| 114 | + <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}> |
| 115 | + <label style={{ minWidth: 120 }}>{dataInput.label}:</label> |
| 116 | + |
| 117 | + {dataInput.type === 'NUMBER' && ( |
| 118 | + <input |
| 119 | + type="range" |
| 120 | + value={dataInput.value} |
| 121 | + min={dataInput.settings?.min ?? 0} |
| 122 | + max={dataInput.settings?.max ?? 100} |
| 123 | + step={dataInput.settings?.step ?? 1} |
| 124 | + onChange={(e) => set(parseFloat(e.target.value))} |
| 125 | + style={{ flex: 1 }} |
| 126 | + /> |
| 127 | + )} |
| 128 | + |
| 129 | + {dataInput.type === 'STRING' && ( |
| 130 | + <input |
| 131 | + type="text" |
| 132 | + value={dataInput.value} |
| 133 | + onChange={(e) => set(e.target.value)} |
| 134 | + style={{ flex: 1, padding: 5 }} |
| 135 | + /> |
| 136 | + )} |
| 137 | + |
| 138 | + {dataInput.type === 'BOOLEAN' && ( |
| 139 | + <input type="checkbox" checked={dataInput.value} onChange={(e) => set(e.target.checked)} /> |
| 140 | + )} |
| 141 | + |
| 142 | + {dataInput.type === 'COLOR' && ( |
| 143 | + <input |
| 144 | + type="color" |
| 145 | + value={dataInput.value} |
| 146 | + onChange={(e) => set(e.target.value)} |
| 147 | + style={{ width: 60, height: 30 }} |
| 148 | + /> |
| 149 | + )} |
| 150 | + |
| 151 | + <span style={{ minWidth: 60, textAlign: 'right', color: '#666' }}>{JSON.stringify(dataInput.value)}</span> |
| 152 | + </div> |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + return null |
| 157 | +} |
0 commit comments