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

feat(useClamp): composable computed usage improvements #2696

Merged
merged 4 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/math/useClamp/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ const min = ref(0)
const max = ref(10)
const value = useClamp(0, min, max)
```

You can also pass a `ref` and the returned `computed` will be updated when the source ref changes:

```ts
import { useClamp } from '@vueuse/math'

const number = ref(0)
const clamped = useClamp(number, 0, 10)
```
40 changes: 39 additions & 1 deletion packages/math/useClamp/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref } from 'vue-demi'
import { computed, isReadonly, ref } from 'vue-demi'
import { useClamp } from '.'

describe('useClamp', () => {
Expand Down Expand Up @@ -49,4 +49,42 @@ describe('useClamp', () => {
v.value = -100
expect(v.value).toBe(-10)
})

it('should work with computed', () => {
const baseRef = ref(10)
const value = computed(() => baseRef.value)
const min = ref(0)
const max = ref(100)

const v = useClamp(value, min, max)

expect(v.value).toBe(10)

baseRef.value = -10
expect(v.value).toBe(0)

baseRef.value = 110
expect(v.value).toBe(100)

expect(isReadonly(v)).toBeTruthy()
})

it('should work with function', () => {
const baseRef = ref(10)
const value = () => baseRef.value
const min = ref(0)
const max = ref(100)

const v = useClamp(value, min, max)

expect(v.value).toBe(10)

baseRef.value = -10
expect(v.value).toBe(0)

baseRef.value = 110
expect(v.value).toBe(100)

expect(isReadonly(v)).toBeTruthy()
})
})
25 changes: 20 additions & 5 deletions packages/math/useClamp/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import type { Ref } from 'vue-demi'
import { computed, ref } from 'vue-demi'
import type { MaybeComputedRef, MaybeRef } from '@vueuse/shared'
import { clamp, resolveUnref } from '@vueuse/shared'
import type { ComputedRef, Ref } from 'vue-demi'
import { computed, isReadonly, ref } from 'vue-demi'
import type { MaybeComputedRef, MaybeReadonlyRef, MaybeRef } from '@vueuse/shared'
import { clamp, isFunction, resolveUnref } from '@vueuse/shared'

export function useClamp(
value: MaybeReadonlyRef<number>,
min: MaybeComputedRef<number>,
max: MaybeComputedRef<number>,
): ComputedRef<number>

export function useClamp(
value: MaybeRef<number>,
min: MaybeComputedRef<number>,
max: MaybeComputedRef<number>,
): Ref<number>

/**
* Reactively clamp a value between two other values.
Expand All @@ -11,7 +23,10 @@ import { clamp, resolveUnref } from '@vueuse/shared'
* @param min
* @param max
*/
export function useClamp(value: MaybeRef<number>, min: MaybeComputedRef<number>, max: MaybeComputedRef<number>): Ref<number> {
export function useClamp(value: MaybeComputedRef<number>, min: MaybeComputedRef<number>, max: MaybeComputedRef<number>) {
if (isFunction(value) || isReadonly(value))
return computed(() => clamp(resolveUnref(value), resolveUnref(min), resolveUnref(max)))

const _value = ref(value)
return computed<number>({
get() {
Expand Down