Leave Yamada UI a star

Star
Yamada UIYamada UIv1.5.3

HueSlider

HueSlider is a component used to allow the user to select a color hue.

Source@yamada-ui/color-picker

Import

import { HueSlider } from "@yamada-ui/react"
Copied!

Usage

Editable example

<HueSlider />
Copied!

Change Size

Editable example

<VStack>
  <HueSlider size="sm" defaultValue={120} />
  <HueSlider size="md" defaultValue={180} />
  <HueSlider size="lg" defaultValue={240} />
</VStack>
Copied!

Set Minimum and Maximum Values

To set minimum and maximum values, assign a number to min or max. By default, min is set to 0 and max is set to 360.

Editable example

const [value, onChange] = useState<number>(140)

return (
  <VStack>
    <Text>Value: {value}</Text>

    <HueSlider value={value} min={120} max={180} onChange={onChange} />
  </VStack>
)
Copied!

Set Step Value

To set a step value, assign a number to step.

Editable example

const [value, onChange] = useState<number>(180)

return (
  <VStack>
    <Text>Value: {value}</Text>

    <HueSlider color="#4387f4" value={value} step={10} onChange={onChange} />
  </VStack>
)
Copied!

Control Externally Only

To control externally only, set focusThumbOnChange to false.

Editable example

const [value, setValue] = useState<number>(180)

return (
  <VStack>
    <Text>Value: {value}</Text>
    <HueSlider value={value} step={10} focusThumbOnChange={false} />

    <Center w="full" gap="md">
      <Button
        isDisabled={value === 0}
        onClick={() => setValue((prev) => (prev !== 0 ? prev - 10 : prev))}
      >
        -10
      </Button>

      <Button
        isDisabled={value === 360}
        colorScheme="blue"
        onClick={() => setValue((prev) => (prev !== 360 ? prev + 10 : prev))}
      >
        +10
      </Button>
    </Center>
  </VStack>
)
Copied!

Disable

To disable, set isDisabled to true.

Editable example

<VStack>
  <HueSlider isDisabled />

  <FormControl
    isDisabled
    label="Pick color"
    helperMessage="Please select your favorite color"
  >
    <HueSlider />
  </FormControl>
</VStack>
Copied!

Make Read-Only

To make read-only, set isReadOnly to true.

Editable example

<VStack>
  <HueSlider isReadOnly />

  <FormControl
    isReadOnly
    label="Pick color"
    helperMessage="Please select your favorite color"
  >
    <HueSlider />
  </FormControl>
</VStack>
Copied!

Handle Start and End Change Events

To handle start and end change events, use onChangeStart and onChangeEnd.

Editable example

const [value, onChange] = useState<number>(180)
const [startValue, onChangeStart] = useState<number>(180)
const [endValue, onChangeEnd] = useState<number>(180)

return (
  <VStack>
    <Text>
      Value: {value}, Start Value: {startValue}, End Value: {endValue}
    </Text>

    <HueSlider
      value={value}
      onChange={onChange}
      onChangeStart={onChangeStart}
      onChangeEnd={onChangeEnd}
    />
  </VStack>
)
Copied!

Use React Hook Form

Editable example

type Data = { hueSlider: number }

const defaultValues: Data = {
  hueSlider: 180,
}

const {
  control,
  handleSubmit,
  watch,
  formState: { errors },
} = useForm<Data>({ defaultValues })

const onSubmit = (data) => console.log("submit:", data)

console.log("watch:", watch())

return (
  <VStack as="form" onSubmit={handleSubmit(onSubmit)}>
    <FormControl
      isInvalid={!!errors.hueSlider}
      label="Pick color"
      errorMessage={errors.hueSlider ? errors.hueSlider.message : undefined}
    >
      <Controller
        name="hueSlider"
        control={control}
        rules={{ max: { value: 180, message: "The maximum value is 180." } }}
        render={({ field }) => <HueSlider {...field} />}
      />
    </FormControl>

    <Button type="submit" alignSelf="flex-end">
      Submit
    </Button>
  </VStack>
)
Copied!

Edit this page on GitHub

PreviousRangeSliderNextAlphaSlider