Color Input Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Take a color as typed hex text. value holds the color as hex, and the valuechange event updates the readout.
The controls switch the text to RGB, HSL, or HSV with format, and add the alpha with alpha. text-transform: uppercase on ::part(hex) shows the hex digits in capitals while value stays lowercase.
value:
--empty
<div class="color-input-basic"> <label for="color-input-color">Color</label> <sv-color-input id="color-input-color" value="#3b82f6"></sv-color-input> <p> value: <code></code> </p></div>
<script type="application/x-typescript" module> const colorInput = document.querySelector<ColorInput>(".color-input-basic sv-color-input")!; const valueOutput = document.querySelector(".color-input-basic code")!;
colorInput.addEventListener("valuechange", () => { valueOutput.textContent = JSON.stringify(colorInput.value); });
valueOutput.textContent = JSON.stringify(colorInput.value);</script>
<style> .color-input-basic { display: grid; gap: 0.5em; place-content: center; justify-items: center;
label { font-weight: 600; }
sv-color-input.uppercase::part(hex) { text-transform: uppercase; }
p { margin: 0; font-size: 0.9em; color: var(--sv-gray-3); } }</style>