@samatawy/diagrams
    Preparing search index...

    Class ColorSelect

    A color selection control that supports predefined options and an optional native color picker input. The control can be configured to show color swatches, labels, and the native input in different arrangements. It dispatches a 'colorchange' event with the selected color whenever the selection changes.

    Index

    Constructors

    Properties

    host: HTMLElement
    selected: string = '#000000'
    config: Required<ColorSelectConfig>
    trigger: HTMLButtonElement
    triggerSwatch?: HTMLDivElement
    triggerLabel?: HTMLSpanElement
    nativeInput?: HTMLInputElement
    customInput?: HTMLInputElement
    customOption?: HTMLButtonElement
    nativeInputMode: "none" | "start" | "end" | "option"
    menu: HTMLDivElement

    Accessors

    Methods

    • Manually adds a color option to the menu. This is useful when the color options are dynamic or not known upfront.

      Parameters

      • color: string

        The color value to add. Any web color or hexadecimal value including alpha channel.

      • Optionallabel: string

        An optional label for the color option.

      Returns void

    • Manually adds multiple color options to the menu. This is useful when the color options are dynamic or not known upfront.

      Parameters

      • colors: (string | ColorOptionInput)[]

        An array of color values or objects containing color and optional label.

      Returns void

    • Removes all color options from the menu, including the transparent 'clear' and custom options if present. The menu will be left empty and the custom option will need to be re-added manually if desired.

      Returns void

    • Handles the click event on the document. Closes the menu if the click is outside the host element.

      Parameters

      • event: Event

      Returns void

    • Handles the click event on a color option. Selects the color and closes the menu.

      Parameters

      • event: Event

      Returns void

    • Builds a preset color option button for the dropdown menu.

      Parameters

      • color: string

        CSS color value for this option.

      • Optionallabel: string

        Optional display label; defaults to the color value.

      Returns HTMLButtonElement

      The constructed option button element.

    • Builds the "custom" option button that opens the native color picker.

      Returns HTMLButtonElement

      The constructed custom option button element.

    • Creates a color swatch element with its background set to the given color.

      Parameters

      • color: string

        CSS color value to display in the swatch.

      Returns HTMLDivElement

      The swatch div element.

    • Creates the multi-colour gradient swatch used for the custom-colour option.

      Returns HTMLDivElement

      The swatch div element.

    • Returns the CSS background value to apply to a swatch element. Transparent colors are rendered as a checkerboard pattern.

      Parameters

      • color: string

        CSS color string.

      Returns string

      The CSS background shorthand value.

    • Returns the human-readable label for a color, showing 'clear' for transparent.

      Parameters

      • color: string

        CSS color string.

      Returns string

      Display label string.

    • Converts a color to a 6-digit hex value accepted by native <input type="color">. Falls back to a dark default when the color cannot be converted.

      Parameters

      • color: string

        CSS color string.

      Returns string

      A #rrggbb hex string.

    • Normalises the showNativeInput config value into one of the four known placement modes.

      Parameters

      • value: "none" | "start" | "end" | "option" | undefined

        The raw config value.

      Returns "none" | "start" | "end" | "option"

      One of 'start', 'end', 'option', or 'none'.

    • Rebuilds basic options in canonical order at the top of the menu: inherit, clear, custom. Existing basic option elements are reused when possible to preserve listeners/state.

      Returns void

    • Returns true when a menu option matching the given color already exists.

      Parameters

      • color: string

        CSS color string to check.

      Returns boolean

      True when the option is present.

    • Normalises a CSS color string to a lowercase 6-digit hex so options can be compared reliably.

      Parameters

      • color: string

        CSS color string to normalise.

      Returns string

      Comparable lowercase color string.

    • Toggles the selected CSS class and aria-selected attribute on all menu option buttons to reflect the current selection.

      Returns void