Skip to content

Commit

Permalink
feat: use-rect package (#274)
Browse files Browse the repository at this point in the history
* feat: use-rect package

* Moved into measurableElement and rect function

* location changed

* use-rect test

* rect test completed

---------

Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
  • Loading branch information
wramzo and dammy001 authored Aug 10, 2023
1 parent f84d75e commit 298b3ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/use-composable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export { useComposedRefs } from './useComposedRefs'
export { useForwardRef } from './useForwardRef'
export { useEscapeKeydown } from './useEscapeKeydown'
export type { MaybeComputedElementRef } from './unrefElement'
export { useRect } from './use-rect'
export { computedEager, syncRef, computedAsync }
29 changes: 29 additions & 0 deletions packages/core/use-composable/src/use-rect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { observeElementRect } from '@oku-ui/utils'
import type { Measurable } from '@oku-ui/utils'

function useRect() {
const measurableElement = ref<Measurable | null>(null)
const rect = ref<DOMRectReadOnly | null>(null)
onMounted(() => {
if (measurableElement.value) {
const unobserve = observeElementRect(measurableElement.value, (newRect) => {
rect.value = newRect
})

onBeforeUnmount(() => {
rect.value = null
unobserve()
})
}
})

return {
measurableElement,
rect,
}
}

export {
useRect,
}
26 changes: 26 additions & 0 deletions packages/core/use-composable/tests/use-rect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { useRect } from '../src/use-rect'

describe('useRect', () => {
it('rect', async () => {
const { rect } = useRect()
expect(rect.value).toBeNull()
})

it('props', () => {
const { measurableElement } = useRect()
expect(measurableElement.value).toBeNull()

const wrapper = mount({
template: '<div></div>',
setup() {
return {
dir: measurableElement,
}
},
})

expect(wrapper.vm.dir).toBeNull()
})
})

0 comments on commit 298b3ec

Please sign in to comment.