Skip to content

Commit

Permalink
feat(useElementBounding): new function
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 4, 2020
1 parent 2deeb6a commit 6fd5935
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE/checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This is a checklist of all the functions of VueUse. It's is useful for review or
- [ ] watchWithFilter
- [ ] when

### core (53)
### core (54)
- [ ] asyncComputed
- [ ] createGlobalState
- [ ] onClickOutside
Expand All @@ -49,6 +49,7 @@ This is a checklist of all the functions of VueUse. It's is useful for review or
- [ ] useDeviceOrientation
- [ ] useDevicePixelRatio
- [ ] useDocumentVisibility
- [ ] useElementBounding
- [ ] useElementSize
- [ ] useElementVisibility
- [ ] useEventListener
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Collection of essential Vue Composition Utilities
<a href="https://www.npmjs.com/package/@vueuse/core" target="__blank"><img src="https://img.shields.io/npm/v/@vueuse/core?color=a1b858&label=" alt="NPM version" /></a>
<a href="https://www.npmjs.com/package/@vueuse/core" target="__blank"><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/@vueuse/core?color=50a36f&label="/></a>
<a href="https://vueuse.js.org" target="__blank"><img src="https://img.shields.io/static/v1?label=&message=docs%20%26%20demos&color=1e8a7a" alt="Storybook Demos" /></a>
<img alt="Function Count" src="https://img.shields.io/badge/-89%20functions-13708a"/>
<img alt="Function Count" src="https://img.shields.io/badge/-90%20functions-13708a"/>
<br>
<a href="https://github.com/antfu/vueuse" target="__blank"><img alt="GitHub stars" src="https://img.shields.io/github/stars/antfu/vueuse?style=social"></a>
</p>
Expand Down Expand Up @@ -98,7 +98,6 @@ You can check out the full documents and live demos in [Storybook](https://vueus
- [`useBrowserLocation`](https://vueuse.js.org/?path=/story/browser--usebrowserlocation) — reactive browser location
- [`useClipboard`](https://vueuse.js.org/?path=/story/browser--useclipboard) — reactive [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API)
- [`useCssVar`](https://vueuse.js.org/?path=/story/browser--usecssvar) — manipulate CSS variables
- [`useElementSize`](https://vueuse.js.org/?path=/story/browser--useelementsize) — reactive size of an HTML element
- [`useEventListener`](https://vueuse.js.org/?path=/story/browser--useeventlistener) — use EventListener with ease
- [`useFavicon`](https://vueuse.js.org/?path=/story/browser--usefavicon) — reactive favicon
- [`useFullscreen`](https://vueuse.js.org/?path=/story/browser--usefullscreen) — reactive [Fullscreen API](https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API)
Expand Down Expand Up @@ -130,6 +129,8 @@ You can check out the full documents and live demos in [Storybook](https://vueus
- [`useDeviceOrientation`](https://vueuse.js.org/?path=/story/sensors--usedeviceorientation) — reactive [DeviceOrientationEvent](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent)
- [`useDevicePixelRatio`](https://vueuse.js.org/?path=/story/sensors--usedevicepixelratio) — reactively track [`window.devicePixelRatio`](https://developer.mozilla.org/ru/docs/Web/API/Window/devicePixelRatio)
- [`useDocumentVisibility`](https://vueuse.js.org/?path=/story/sensors--usedocumentvisibility) — reactively track [`document.visibilityState`](https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState)
- [`useElementBounding`](https://vueuse.js.org/?path=/story/sensors--useelementbounding) — reactive [bounding box](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) of an HTML element
- [`useElementSize`](https://vueuse.js.org/?path=/story/sensors--useelementsize) — reactive size of an HTML element
- [`useElementVisibility`](https://vueuse.js.org/?path=/story/sensors--useelementvisibility) — tracks the visibility of an element within the viewport
- [`useGeolocation`](https://vueuse.js.org/?path=/story/sensors--usegeolocation) — reactive [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API)
- [`useIdle`](https://vueuse.js.org/?path=/story/sensors--useidle) — tracks whether the user is being inactive
Expand Down
21 changes: 14 additions & 7 deletions indexes.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@
"description": "reactively track [`document.visibilityState`](https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState)",
"depreacted": false
},
{
"name": "useElementBounding",
"category": "Sensors",
"docs": "https://vueuse.js.org/?path=/story/sensors--useelementbounding",
"description": "reactive [bounding box](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) of an HTML element",
"depreacted": false
},
{
"name": "useElementSize",
"category": "Sensors",
"docs": "https://vueuse.js.org/?path=/story/sensors--useelementsize",
"description": "reactive size of an HTML element",
"depreacted": false
},
{
"name": "useElementVisibility",
"category": "Sensors",
Expand Down Expand Up @@ -435,13 +449,6 @@
"description": "manipulate CSS variables",
"depreacted": false
},
{
"name": "useElementSize",
"category": "Browser",
"docs": "https://vueuse.js.org/?path=/story/browser--useelementsize",
"description": "reactive size of an HTML element",
"depreacted": false
},
{
"name": "useEventListener",
"category": "Browser",
Expand Down
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './useDeviceMotion'
export * from './useDeviceOrientation'
export * from './useDevicePixelRatio'
export * from './useDocumentVisibility'
export * from './useElementBounding'
export * from './useElementSize'
export * from './useElementVisibility'
export * from './useEventListener'
Expand Down
28 changes: 28 additions & 0 deletions packages/core/useElementBounding/index.md
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>
```
34 changes: 34 additions & 0 deletions packages/core/useElementBounding/index.stories.tsx
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>
`,
}),
)
51 changes: 51 additions & 0 deletions packages/core/useElementBounding/index.ts
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,
}
}
2 changes: 1 addition & 1 deletion packages/core/useElementSize/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# useElementSize

> Reactive size of an HTML element. (simple version of `useResizeObserver`)
> Reactive size of an HTML element.
## Usage

Expand Down
2 changes: 1 addition & 1 deletion packages/core/useElementSize/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useElementSize } from '.'
defineDemo(
{
name: 'useElementSize',
category: 'Browser',
category: 'Sensors',
docs: require('./index.md'),
module,
},
Expand Down

0 comments on commit 6fd5935

Please sign in to comment.