forked from CyCraft/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useElementBounding): new function
- Loading branch information
Showing
9 changed files
with
135 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# useElementBounding | ||
|
||
> Reactive [bounding box](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) of an HTML element | ||
## Usage | ||
|
||
```html | ||
<template> | ||
<div ref="el" /> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue' | ||
import { useElementBounding } from '@vueuse/core' | ||
export default { | ||
setup() { | ||
const el = ref(null) | ||
const { x, y, top, right, bottom, left, width, height } = useElementBounding(el) | ||
return { | ||
el, | ||
/* ... */ | ||
} | ||
} | ||
}) | ||
</script> | ||
``` |
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,34 @@ | ||
import { defineDemo, html } from '../../_docs' | ||
import { defineComponent, reactive, ref } from 'vue-demi' | ||
import { useElementBounding } from '.' | ||
|
||
defineDemo( | ||
{ | ||
name: 'useElementBounding', | ||
category: 'Sensors', | ||
docs: require('./index.md'), | ||
module, | ||
}, | ||
defineComponent({ | ||
setup() { | ||
const el = ref(null) | ||
const rect = reactive(useElementBounding(el)) | ||
|
||
return { | ||
el, | ||
rect, | ||
} | ||
}, | ||
|
||
template: html` | ||
<div style="min-height: 400px"> | ||
<pre | ||
ref="el" | ||
class="resize bg-gray-600 bg-opacity-25 select-none p-4 overflow-auto" | ||
style="width: 300px; height: 300px;" | ||
>{{rect}} | ||
</pre> | ||
</div> | ||
`, | ||
}), | ||
) |
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 @@ | ||
import { MaybeRef } from '@vueuse/shared' | ||
import { ref } from 'vue-demi' | ||
import { ResizeObserverOptions, useResizeObserver } from '../useResizeObserver' | ||
|
||
/** | ||
* Reactive size of an HTML element. | ||
* | ||
* @see {@link https://vueuse.js.org/useElementSize} | ||
* @param target | ||
* @param callback | ||
* @param options | ||
*/ | ||
export function useElementBounding( | ||
target: MaybeRef<Element | null | undefined>, | ||
options: ResizeObserverOptions = {}, | ||
) { | ||
const height = ref(0) | ||
const bottom = ref(0) | ||
const left = ref(0) | ||
const right = ref(0) | ||
const top = ref(0) | ||
const width = ref(0) | ||
const x = ref(0) | ||
const y = ref(0) | ||
|
||
useResizeObserver( | ||
target, | ||
([entry]) => { | ||
height.value = entry.contentRect.height | ||
bottom.value = entry.contentRect.bottom | ||
left.value = entry.contentRect.left | ||
right.value = entry.contentRect.right | ||
top.value = entry.contentRect.top | ||
width.value = entry.contentRect.width | ||
x.value = entry.contentRect.x | ||
y.value = entry.contentRect.y | ||
}, | ||
options, | ||
) | ||
|
||
return { | ||
x, | ||
y, | ||
top, | ||
right, | ||
bottom, | ||
left, | ||
width, | ||
height, | ||
} | ||
} |
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