-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(useElementOverflow): new function #4094
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
Open
inside5545
wants to merge
17
commits into
vueuse:main
Choose a base branch
from
inside5545:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ba3143c
feat(useElementOverflow): new function
038922e
feat(useElementOverflow): new function
d9163ea
Merge branch 'vueuse:main' into main
inside5545 3995276
feat(useElementOverflow): new function
5bd22c5
Merge branch 'main' into main
inside5545 8c83a7d
Merge branch 'main' into main
antfu 5c1a103
Merge branch 'vueuse:main' into main
inside5545 9d2905e
feat(useElementOverflow): add unit tests
fe6dbc5
feat(useElementOverflow): add unit tests
297721b
feat(useElementOverflow): add unit tests
8d858ca
[autofix.ci] apply automated fixes
autofix-ci[bot] ffec6f9
Merge branch 'main' into main
inside5545 e7806a2
feat(useElementOverflow): change import
2d609da
Merge branch 'main' into main
ilyaliao 93bb221
fix: lint
ilyaliao 998c76b
test: update exports snapshot
ilyaliao 293bec7
Merge branch 'main' into main
ilyaliao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(useElementOverflow): new function
- Loading branch information
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
import { defineComponent, h, reactive, ref } from 'vue-demi' | ||
import { type RenderableComponent, type UseElementOverflowOptions, useElementOverflow } from '@vueuse/core' | ||
|
||
export const UseElementOverflow = /* #__PURE__ */ defineComponent<UseElementOverflowOptions & RenderableComponent>({ | ||
name: 'UseElementOverflow', | ||
props: ['observeMutation', 'as'] as unknown as undefined, | ||
emits: ['update'], | ||
setup(props, { slots, attrs, emit }) { | ||
const target = ref() | ||
const info = reactive(useElementOverflow(target, { | ||
observeMutation: props.observeMutation, | ||
onUpdated: (...args: any[]) => { emit('update', ...args) }, | ||
})) | ||
return () => { | ||
if (slots.default) | ||
return h(props.as || 'div', { ref: target, ...attrs }, slots.default(info)) | ||
} | ||
}, | ||
}) |
This file contains hidden or 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,28 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue-demi' | ||
import { useElementOverflow } from './index' | ||
|
||
const overflowRef = ref() | ||
const { isXOverflowed } = useElementOverflow(overflowRef, { observeMutation: true }) | ||
const input = ref('some words here') | ||
const width = ref(200) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div>content:</div> | ||
<input v-model="input" type="text"> | ||
<div>width:</div> | ||
<input v-model.number="width" type="range" min="0" step="1" max="200"> | ||
<div>display:</div> | ||
<div ref="overflowRef" :style="`width: ${width}px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;`"> | ||
<b>{{ input }}</b> | ||
</div> | ||
<br> | ||
<div> | ||
isOverflowed: <boolean-display :value="isXOverflowed"> | ||
{{ isXOverflowed }} | ||
</boolean-display> | ||
</div> | ||
</div> | ||
</template> |
This file contains hidden or 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,19 @@ | ||
import { watch } from 'vue-demi' | ||
import { directiveHooks } from '@vueuse/shared' | ||
import type { ObjectDirective } from 'vue-demi' | ||
import { type UseElementOverflowOptions, type UseElementOverflowReturn, useElementOverflow } from '.' | ||
|
||
type VElementOverflowHandler = (info: UseElementOverflowReturn) => void | ||
|
||
export const vElementOverflow: ObjectDirective< | ||
HTMLElement, | ||
VElementOverflowHandler | [VElementOverflowHandler, UseElementOverflowOptions] | ||
> = { | ||
[directiveHooks.mounted](el, binding) { | ||
const bindingValue = typeof binding.value === 'function' ? [binding.value] as [VElementOverflowHandler] : binding.value | ||
const [handler, options] = bindingValue | ||
const info = useElementOverflow(el, options) | ||
const { isXOverflowed, isYOverflowed } = info | ||
watch([isXOverflowed, isYOverflowed], () => handler(info), { immediate: true }) | ||
}, | ||
} |
This file contains hidden or 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,77 @@ | ||
--- | ||
category: Elements | ||
--- | ||
|
||
# useElementOverflow | ||
|
||
Reactive element's overflow state. | ||
|
||
## Usage | ||
|
||
```vue | ||
<script> | ||
import { ref } from 'vue' | ||
import { useElementOverflow } from '@vueuse/core' | ||
|
||
export default { | ||
setup() { | ||
const el = ref() | ||
const { isXOverflowed } = useElementOverflow(el) | ||
return { | ||
isXOverflowed | ||
} | ||
}, | ||
} | ||
</script> | ||
|
||
<template> | ||
<div ref="el" style="width: 100px;overflow: hidden;"> | ||
<button v-if="isXOverflowed"> | ||
show more | ||
</button> | ||
<span v-else>some words may be too long to show here</span> | ||
</div> | ||
</template> | ||
``` | ||
|
||
## Component Usage | ||
|
||
```vue | ||
<template> | ||
<UseElementOverflow v-slot="{ isXOverflowed }" style="width: 100px;overflow: hidden;"> | ||
<button v-if="isXOverflowed"> | ||
show more | ||
</button> | ||
<span v-else>some words may be too long to show here</span> | ||
</UseElementOverflow> | ||
</template> | ||
``` | ||
|
||
## Directive Usage | ||
|
||
```vue | ||
<script> | ||
import { ref } from 'vue' | ||
|
||
export default { | ||
setup() { | ||
const isXOverflowed = ref(false) | ||
function onXOverflowChanged(info) { | ||
isXOverflowed.value = info.isXOverflowed | ||
} | ||
return { | ||
isXOverflowed | ||
} | ||
}, | ||
} | ||
</script> | ||
|
||
<template> | ||
<div v-element-overflow="onXOverflowChanged" style="width: 100px;overflow: hidden;"> | ||
<button v-if="isXOverflowed"> | ||
show more | ||
</button> | ||
<span v-else>some words may be too long to show here</span> | ||
</div> | ||
</template> | ||
``` |
This file contains hidden or 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,80 @@ | ||
import { effectScope, ref } from 'vue-demi' | ||
import { | ||
tryOnMounted, | ||
tryOnUnmounted, | ||
} from '@vueuse/shared' | ||
import type { ConfigurableWindow } from '@vueuse/core/_configurable' | ||
import { type MaybeComputedElementRef, unrefElement } from '../unrefElement' | ||
import type { ResizeObserverCallback } from '../useResizeObserver' | ||
import { useMutationObserver } from '../useMutationObserver' | ||
import { useResizeObserver } from '../useResizeObserver' | ||
import { defaultWindow } from '../_configurable' | ||
|
||
export interface UseElementOverflowOptions extends ConfigurableWindow { | ||
observeMutation?: boolean | MutationObserverInit | ||
onUpdated?: ResizeObserverCallback | MutationCallback | ||
} | ||
|
||
export function useElementOverflow(target: MaybeComputedElementRef, option: UseElementOverflowOptions = {}) { | ||
const { observeMutation, onUpdated, window = defaultWindow } = option | ||
const isXOverflowed = ref(false) | ||
const isYOverflowed = ref(false) | ||
|
||
const scope = effectScope() | ||
|
||
function update(htmlEl: HTMLElement) { | ||
isXOverflowed.value = htmlEl.scrollWidth > htmlEl.offsetWidth | ||
isYOverflowed.value = htmlEl.scrollHeight > htmlEl.offsetHeight | ||
} | ||
|
||
function stop() { | ||
scope.stop() | ||
} | ||
|
||
tryOnMounted(() => { | ||
const el = unrefElement(target) | ||
if (!el || el instanceof SVGElement || !window) { | ||
return | ||
} | ||
scope.run(() => { | ||
if (!el || el instanceof SVGElement) { | ||
return | ||
} | ||
const childEls = Array.from(el.children).filter(i => i instanceof HTMLElement) | ||
useResizeObserver([el, ...childEls], (entries, observer) => { | ||
update(el); | ||
(onUpdated as ResizeObserverCallback)?.(entries, observer) | ||
}) | ||
if (observeMutation) { | ||
useMutationObserver( | ||
[el, ...childEls], | ||
(entries, observer) => { | ||
update(el); | ||
(onUpdated as MutationCallback)?.(entries, observer) | ||
}, | ||
observeMutation === true | ||
? { | ||
childList: true, | ||
subtree: true, | ||
characterData: true, | ||
} | ||
: observeMutation, | ||
) | ||
} | ||
}) | ||
}) | ||
tryOnUnmounted(stop) | ||
return { | ||
isXOverflowed, | ||
isYOverflowed, | ||
stop, | ||
update: () => { | ||
const el = unrefElement(target) | ||
if (el instanceof HTMLElement && window) { | ||
update(el) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export type UseElementOverflowReturn = ReturnType<typeof useElementOverflow> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some description for it ? and options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zyyv Thanks,I added some for it.