Skip to content

Commit

Permalink
fix(useDraggable): write to useStorage (#2575)
Browse files Browse the repository at this point in the history
  • Loading branch information
akifo committed Jan 4, 2023
1 parent c363935 commit e39c2a7
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/core/useDraggable/component.ts
Expand Up @@ -2,7 +2,7 @@ import { computed, defineComponent, h, reactive, ref } from 'vue-demi'
import type { UseDraggableOptions } from '@vueuse/core'
import { isClient, useDraggable, useStorage } from '@vueuse/core'
import { resolveUnref } from '@vueuse/shared'
import type { RenderableComponent } from '../types'
import type { Position, RenderableComponent } from '../types'

export interface UseDraggableProps extends UseDraggableOptions, RenderableComponent {
/**
Expand Down Expand Up @@ -34,22 +34,28 @@ export const UseDraggable = defineComponent<UseDraggableProps>({
setup(props, { slots }) {
const target = ref()
const handle = computed(() => props.handle ?? target.value)
const initialValue = props.storageKey
? useStorage(
props.storageKey,
resolveUnref(props.initialValue) || { x: 0, y: 0 },
isClient
? props.storageType === 'session'
? sessionStorage
: localStorage
: undefined,
)
: props.initialValue || { x: 0, y: 0 }
const storageValue = props.storageKey && useStorage(
props.storageKey,
resolveUnref(props.initialValue) || { x: 0, y: 0 },
isClient
? props.storageType === 'session'
? sessionStorage
: localStorage
: undefined,
)
const initialValue = storageValue || props.initialValue || { x: 0, y: 0 }
const onEnd = (position: Position) => {
if (!storageValue)
return
storageValue.value.x = position.x
storageValue.value.y = position.y
}

const data = reactive(useDraggable(target, {
...props,
handle,
initialValue,
onEnd,
}))

return () => {
Expand Down

0 comments on commit e39c2a7

Please sign in to comment.