Skip to content

Commit

Permalink
feat: make options placement and offset of useLocated reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
s3xysteak committed Oct 11, 2024
1 parent e08510f commit 0ce535f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/core/components/Located/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ viewer.entities.add({
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
})
const left = ref(0)
const top = ref(0)
</script>

<template>
Expand All @@ -55,13 +58,24 @@ viewer.entities.add({
<button ml-4 btn @click="state = !state">
Now {{ state ? 'Show' : 'Hide' }}
</button>

<label>
left :
<input v-model="left" type="number">
</label>

<label>
top :
<input v-model="top" type="number">
</label>
</div>

<Located
v-model="state"
:as="MyButton"
placement="top"
:coordinate="coordinate"
:offset="{ left, top }"
>
<div>
Hello world!
Expand Down
16 changes: 11 additions & 5 deletions src/core/components/Located/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Nullable } from '@s3xysteak/utils'
import type { MaybeCoordinates } from '~shared/coordinate'
import type { Cartesian3 } from 'cesium'
import { type Component, ref } from 'vue'
import { useLocated, type UseLocatedOptions } from '~/index'
import { useLocated, type UseLocatedPlacement } from '~/index'
defineOptions({
name: 'Located',
Expand All @@ -12,9 +12,12 @@ defineOptions({
const props = withDefaults(defineProps<{
coordinate: Nullable<Cartesian3 | MaybeCoordinates>
placement?: UseLocatedOptions['placement']
placement?: Nullable<UseLocatedPlacement>
offset?: UseLocatedOptions['offset']
offset?: {
left?: Nullable<number>
top?: Nullable<number>
}
as?: string | Component
}>(), {
Expand All @@ -28,8 +31,11 @@ const el = ref()
const { style, show } = useLocated(el, {
state,
coordinate: () => props.coordinate,
offset: props.offset,
placement: props.placement,
offset: {
left: () => props.offset?.left,
top: () => props.offset?.top,
},
placement: () => props.placement,
})
</script>

Expand Down
20 changes: 11 additions & 9 deletions src/core/composables/useLocated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { toRefs, useElementBounding } from '@vueuse/core'
import { computed, type MaybeRefOrGetter, type Ref, ref, toValue } from 'vue'
import { toCartesian3, useEventHandler, useViewer } from '~/index'

type Placement =
export type UseLocatedPlacement =
| 'topLeft'
| 'top'
| 'topRight'
Expand All @@ -27,10 +27,10 @@ export interface UseLocatedOptions {
/**
* @default 'bottomRight'
*/
placement: Placement
placement: MaybeRefOrGetter<Nullable<UseLocatedPlacement>>
offset: {
left?: number
top?: number
left?: MaybeRefOrGetter<Nullable<number>>
top?: MaybeRefOrGetter<Nullable<number>>
}
}

Expand All @@ -40,11 +40,13 @@ export interface UseLocatedOptions {
* Component version please use `import { Located } from 'cesium-use'`
*/
export function useLocated(el: MaybeRefOrGetter<Nullable<HTMLElement>>, options: Partial<UseLocatedOptions> = {}) {
const defaultPlacement = 'bottomRight'

const {
state: _state,
coordinate: _coordinate,
initialValue = true,
placement = 'bottomRight',
placement,
offset = {},
} = options

Expand All @@ -57,7 +59,7 @@ export function useLocated(el: MaybeRefOrGetter<Nullable<HTMLElement>>, options:

const { width, height } = useElementBounding(el)

const placementMap: Record<Placement, (rect: { width: number, height: number }) => { width: number, height: number }> = {
const placementMap: Record<UseLocatedPlacement, (rect: { width: number, height: number }) => { width: number, height: number }> = {
topLeft: rect => ({
width: -rect.width,
height: -rect.height,
Expand Down Expand Up @@ -91,14 +93,14 @@ export function useLocated(el: MaybeRefOrGetter<Nullable<HTMLElement>>, options:
}
__show.value = true

const offsetPlacement = placementMap[placement]({
const offsetPlacement = placementMap[toValue(placement) ?? defaultPlacement]({
width: width.value,
height: height.value,
})

position.value = {
x: pos.x + (offsetPlacement.width ?? 0) + (offset.left ?? 0),
y: pos.y + (offsetPlacement.height ?? 0) + (offset.top ?? 0),
x: pos.x + (offsetPlacement.width ?? 0) + (toValue(offset.left) ?? 0),
y: pos.y + (offsetPlacement.height ?? 0) + (toValue(offset.top) ?? 0),
}
})

Expand Down

0 comments on commit 0ce535f

Please sign in to comment.