ColorPalette
ColorPalette
is a UI widget for displaying dropdowns with a color palette.
Learn more about the ColorPalette plugin.
constructorβ
constructor(opt?: ColorPalette.Options);
The ColorPalette
constructor accepts several parameters:
widthβ
[optional] The width of the color palette in pixels.
optionsβ
[optional] An array of items. Each item is an object with the following properties:
contentβ
The color in hex or other color formats (rgb, rgba).
iconβ
A path to an image (note that it can also be an image embedded via the Data URI Scheme). The icon can be handy for representing the transparent color.
selectedβ
true
if the item should be selected by default. If none of the items are selected, the first item is selected by default.
selectedβ
[optional] The index of the item which should be selected by default. If this value is undefined
(which it is by default),
the ui.ColorPalette
widget looks up the selected item from the options
array (by using the selected
boolean property).
If nothing is selected, the first item in the options
array is selected by default.
keyboardNavigationβ
[optional] true
(the default) if you want the ui.ColorPalette
to provide a keyboard navigation
(up/down/left/right/enter/escape keys are supported).
selectBoxOptionsClassβ
[optional] When the color palette is opened, the ui.ColorPalette
generates a HTML container that contains all the
items. This container is then appended to the document body (or target
if provided). Sometimes, you want to provide
custom styling to this color palette container. selectBoxOptionsClass
gives you a chance to define a CSS class that
will be set on this container, so that you can style it in your CSS.
targetβ
[optional] ui.ColorPalette
generates a HTML container that contains all the color palette items. This container is
by default appended to the document body. In some situations (e.g. if you want the color palette to scroll with some
other container), you want to append this container to a different DOM element. The target
option is exactly for that.
It accepts a HTML element that will be used as a container for the generated color palette items.
placeholderβ
[optional] In some cases, you want to "deselect" the color palette, and show a placeholder in place of the selected
item. This is when you don't want any of the items to be selected. In this case, set the selected
index to -1
, and
the placeholder
to any HTML you want to display in the selected item window. This placeholder has the
selected-box-placeholder
CSS class that you can use for further styling.
Methodsβ
render()β
colorPalette.render(): this;
Render the color palette. Note that once you render the color palette, you can use the el
property that points to the
container HTML element, and append it anywhere in the DOM (e.g. document.body.appendChild(colorPalette.el)
).
getSelection()β
colorPalette.getSelection(): SelectBox.Selection;
getSelectionValue()β
colorPalette.getSelectionValue(selection?: SelectBox.Selection): any;
Get the current selection value (the color).
getSelectionIndex()β
colorPalette.getSelectionIndex(): number;
Get the index in the options
array of the item that is currently selected.
select()β
colorPalette.select(idx: number, opt?: { [key: string]: any }): void;
Select an item. idx
is the index to the options
array. opt
is optional and can be an arbitrary object that will
be passed to the option:select
event handler.
selectByValue()β
colorPalette.selectByValue(value: any, opt?: { [key: string]: any }): void;
Select an item by color.
isOpen()β
colorPalette.isOpen(): boolean;
Returns true
if the color palette is currently opened. It is false
otherwise.
toggle()β
colorPalette.toggle(): void;
Programmatically toggle the color palette.
open()β
colorPalette.open(): void;
Programmatically open the color palette.
close()β
colorPalette.close(): void;
Programmatically close the color palette.
remove()β
colorPalette.remove(): this;
Remove the color palette from the DOM.
on()β
colorPalette..on(eventName: string, callback: EventHandler, context?: any): this;
Register a handler function that will be called whenever eventName
is triggered. The optional context
is an object
that will be the context of the handler function (the this
).
Eventsβ
option:hoverβ
Triggered when the user hovers over one of the color items. The handler is passed the option object, and the index of the
option in the options
array.
option:selectβ
Triggered when the user selects an item. The handler is passed the option object, and the index of the option in the
options
array. If you selected the item with the select(index, opt)
method, the third argument to the event handler
will be your opt
object.
option:mouseoutβ
Triggered when the user leaves an item with mouse cursor. The handler is passed an event object as the only argument.
closeβ
Triggered when the color palette gets closed.
Typesβ
Optionsβ
ColorPalette.Options
extends SelectBox.Options
.