Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs on listening to events for color picker #22

Open
wants to merge 1 commit into
base: 0.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions color-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ For a full reference of all options, please consult [the Pickr documentation](ht

> Please note that only scalar values are supported. You cannot use any JavaScript language specific options like callbacks.

## Listening for events

The `color-picker` component emits two events:

- `buk:color-save` when Pickr emits the `save` event. When the user clicks save/clear.
- `buk:color-change` on the Pickr `change` event. Emits when the color has changed (but not saved).

*You can read more about the Pickr `save` and `change` events in the [Pickr documentation](https://github.com/Simonwep/pickr?tab=readme-ov-file#events).*

In both events, the color value is contained in the `.detail.color` property of the event. And the id of the color picker is contained in the `.detail.id` property.

To listen to the events you can use [Alpine.js](https://alpinejs.dev/directives/on):

```html
<x-color-picker name="color" @buk:color-save="console.log(`Saved to ${$event.detail.color}`)" />

<x-color-picker name="color" @buk:color-change="console.log(`${$event.detail.id} changed!`)" />
```

Or vanilla JavaScript:

```js
window.addEventListener('buk:color-change', function(event) {
console.log(`Color changed to ${event.detail.color} in element:`, event.target);
});
```

## Setting Defaults

If you'd like to set some sensible defaults for all your `color-picker` component usages you can do so by overwriting the component class and `options` method:
Expand Down