forked from vexip-ui/vexip-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v-resize): add
v-resize
directive (vexip-ui#457)
* types(v-loading): improve type definition for binding.value * fix(v-loading): auto create component on updated if not mounted * feat: create directive * docs: update
- Loading branch information
Showing
11 changed files
with
301 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { vLoading } from './loading' | ||
import { vResize } from './resize' | ||
|
||
import type { App } from 'vue' | ||
|
||
export function install(app: App) { | ||
export function installDirectives(app: App) { | ||
app.directive('loading', vLoading) | ||
app.directive('resize', vResize) | ||
} | ||
|
||
export * from './loading' | ||
export * from './resize' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useResize } from '@vexip-ui/hooks' | ||
import { noop, throttle } from '@vexip-ui/utils' | ||
|
||
import type { DirectiveBinding, ObjectDirective } from 'vue' | ||
import type { ResizeHandler } from '@vexip-ui/hooks' | ||
|
||
const { observeResize, unobserveResize } = useResize() | ||
|
||
export interface VResizeOptions { | ||
handler: ResizeHandler, | ||
throttle?: boolean | number, | ||
disabled?: boolean | ||
} | ||
|
||
interface ResizeRecord { | ||
useThrottle: boolean | number, | ||
observed: boolean | ||
} | ||
|
||
function createObserver( | ||
el: HTMLElement & { __resize?: ResizeRecord }, | ||
binding: DirectiveBinding<ResizeHandler | VResizeOptions> | ||
) { | ||
const options: VResizeOptions = | ||
typeof binding.value === 'function' ? { handler: binding.value } : { ...binding.value } | ||
const useThrottle = options.throttle || binding.modifiers.throttle | ||
|
||
el.__resize = { | ||
useThrottle, | ||
observed: false | ||
} | ||
|
||
if (options.disabled) { | ||
unobserveResize(el) | ||
el.__resize.observed = false | ||
return | ||
} | ||
|
||
const throttleResize = useThrottle | ||
? throttle(options.handler, typeof useThrottle === 'boolean' ? 16 : useThrottle) | ||
: options.handler | ||
|
||
observeResize(el, throttleResize) | ||
el.__resize.observed = true | ||
} | ||
|
||
export const vResize: ObjectDirective< | ||
HTMLElement & { __resize?: ResizeRecord }, | ||
ResizeHandler | VResizeOptions | ||
> = { | ||
mounted(el, binding) { | ||
createObserver(el, binding) | ||
}, | ||
updated(el, binding) { | ||
if (!el.__resize) { | ||
createObserver(el, binding) | ||
return | ||
} | ||
|
||
const options: VResizeOptions = | ||
typeof binding.value === 'function' ? { handler: binding.value } : { ...binding.value } | ||
const useThrottle = options.throttle || binding.modifiers.throttle | ||
|
||
const getHandler = () => | ||
useThrottle | ||
? throttle(options.handler, typeof useThrottle === 'boolean' ? 16 : useThrottle) | ||
: options.handler | ||
|
||
if (options.disabled) { | ||
if (el.__resize.observed) { | ||
unobserveResize(el) | ||
el.__resize.observed = false | ||
} | ||
} else if (!el.__resize.observed) { | ||
observeResize(el, getHandler()) | ||
el.__resize.observed = true | ||
} else { | ||
const prevOptions: VResizeOptions = | ||
typeof binding.oldValue === 'function' | ||
? { handler: binding.oldValue } | ||
: { ...(binding.oldValue || { handler: noop }) } | ||
|
||
if (useThrottle !== el.__resize.useThrottle || options.handler !== prevOptions.handler) { | ||
unobserveResize(el) | ||
observeResize(el, getHandler()) | ||
} | ||
} | ||
}, | ||
beforeUnmount(el) { | ||
if (el.__resize?.observed) { | ||
unobserveResize(el) | ||
} | ||
|
||
delete el.__resize | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<template> | ||
<div v-resize="handleResize" class="resizable-pane"> | ||
<span> | ||
Width: {{ width }}px {{ width === 1000 ? '(Max)' : width === 200 ? '(Min)' : '' }} | ||
</span> | ||
<span> | ||
Height: {{ height }}px {{ height === 600 ? '(Max)' : height === 80 ? '(Min)' : '' }} | ||
</span> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const width = ref(0) | ||
const height = ref(0) | ||
function handleResize(entry: ResizeObserverEntry) { | ||
console.info('toggle resize') | ||
const box = entry.borderBoxSize?.[0] | ||
if (box) { | ||
width.value = box.inlineSize | ||
height.value = box.blockSize | ||
} else { | ||
width.value = entry.contentRect.width | ||
height.value = entry.contentRect.height | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
.resizable-pane { | ||
display: inline-flex; | ||
flex-direction: column; | ||
width: 300px; | ||
min-width: 200px; | ||
max-width: 1000px; | ||
height: 200px; | ||
min-height: 80px; | ||
max-height: 600px; | ||
padding: 10px; | ||
overflow: auto; | ||
color: #fff; | ||
resize: both; | ||
background-color: var(--vxp-color-primary-base); | ||
border: var(--vxp-border-base); | ||
border-radius: var(--vxp-border-radius-large); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<template> | ||
<div v-resize="handleResize" class="resizable-pane"> | ||
<span> | ||
Width: {{ width }}px {{ width === 1000 ? '(Max)' : width === 200 ? '(Min)' : '' }} | ||
</span> | ||
<span> | ||
Height: {{ height }}px {{ height === 600 ? '(Max)' : height === 80 ? '(Min)' : '' }} | ||
</span> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
const width = ref(0) | ||
const height = ref(0) | ||
function handleResize(entry: ResizeObserverEntry) { | ||
console.info('toggle resize') | ||
const box = entry.borderBoxSize?.[0] | ||
if (box) { | ||
width.value = box.inlineSize | ||
height.value = box.blockSize | ||
} else { | ||
width.value = entry.contentRect.width | ||
height.value = entry.contentRect.height | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
.resizable-pane { | ||
display: inline-flex; | ||
flex-direction: column; | ||
width: 300px; | ||
min-width: 200px; | ||
max-width: 1000px; | ||
height: 200px; | ||
min-height: 80px; | ||
max-height: 600px; | ||
padding: 10px; | ||
overflow: auto; | ||
color: #fff; | ||
resize: both; | ||
background-color: var(--vxp-color-primary-base); | ||
border: var(--vxp-border-base); | ||
border-radius: var(--vxp-border-radius-large); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ | |
|
||
:::demo spin/directive | ||
|
||
### 指令加载中 | ||
### 使用指令 | ||
|
||
使用 `v-loading` 指令可以快速为元素添加加载中状态。 | ||
|
||
|
Oops, something went wrong.