Skip to content

Commit

Permalink
feat(slider): add onValueCommitted callback (#1212)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas G. Lopes <26071571 [email protected]>
  • Loading branch information
mikededo and TGlide authored May 29, 2024
1 parent f6e6f76 commit 8e9273a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-poems-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
---
'@melt-ui/svelte': minor
---

feat(slider): add `onValueCommitted` callback
5 changes: 5 additions & 0 deletions src/docs/data/builders/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 72,11 @@ const builder = builderSchema(BUILDER_NAME, {
description: 'A callback that is called when the value of the slider changes.',
see: SEE.CHANGE_FUNCTIONS,
},
{
name: 'onValueCommitted',
type: '(value: number[]) => void',
description: 'The callback invoked when the user has committed the value of the slider.',
},
],
elements: [
{
Expand Down
12 changes: 11 additions & 1 deletion src/lib/builders/slider/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 42,9 @@ const { name } = createElHelpers('slider');
export const createSlider = (props?: CreateSliderProps) => {
const withDefaults = { ...defaults, ...props } satisfies CreateSliderProps;

const options = toWritableStores(omit(withDefaults, 'value', 'onValueChange', 'defaultValue'));
const options = toWritableStores(
omit(withDefaults, 'value', 'onValueChange', 'onValueCommitted', 'defaultValue')
);
const { min, max, step, orientation, dir, disabled, autoSort } = options;

const valueWritable = withDefaults.value ?? writable(withDefaults.defaultValue);
Expand Down Expand Up @@ -323,6 325,10 @@ export const createSlider = (props?: CreateSliderProps) => {
break;
}
}

if (withDefaults?.onValueCommitted) {
withDefaults.onValueCommitted(value.get());
}
});

return {
Expand Down Expand Up @@ -516,6 522,10 @@ export const createSlider = (props?: CreateSliderProps) => {

const pointerUp = () => {
isActive.set(false);

if (withDefaults?.onValueCommitted) {
withDefaults.onValueCommitted(value.get());
}
};

const unsub = executeCallbacks(
Expand Down
5 changes: 5 additions & 0 deletions src/lib/builders/slider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 27,11 @@ export type CreateSliderProps = {
*/
onValueChange?: ChangeFn<number[]>;

/**
* The callback invoked when the user has committed the value of the slider.
*/
onValueCommitted?: (value: number[]) => void;

/**
* The minimum value of the slider.
*
Expand Down

0 comments on commit 8e9273a

Please sign in to comment.