Skip to content
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

Enhancement: More positions! #857

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions demo/components/PositionSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,38 @@
<script lang="ts" setup>
import PSelect from '@/components/Select/PSelect.vue'
import { PositionMethod } from '@/types/position'
import { left, right, bottom, top, topRight, bottomRight, topLeft, bottomLeft } from '@/utilities/position'

/* eslint import/namespace: ['error', { allowComputed: true }] */
import * as positions from '@/utilities/position'
import { computed } from 'vue'

type PositionsKey = keyof typeof positions

const props = defineProps<{
// using any because vue's type warnings for props is dumb...
// eslint-disable-next-line @typescript-eslint/no-explicit-any
position: any,
position: PositionMethod | PositionMethod[],
Comment on lines -14 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

}>()

const emit = defineEmits<{
(event: 'update:position', value: PositionMethod | PositionMethod[]): void,
}>()

const keyToFunction = new Map([
['left', left],
['right', right],
['bottom', bottom],
['top', top],
['top right', topRight],
['bottom right', bottomRight],
['top left', topLeft],
['bottom left', bottomLeft],
])

const functionToKey = new Map([
[left, 'left'],
[right, 'right'],
[bottom, 'bottom'],
[top, 'top'],
[topRight, 'top right'],
[bottomRight, 'bottom right'],
[topLeft, 'top left'],
[bottomLeft, 'bottom left'],
])

const options = Array.from(keyToFunction.keys())
const options = Object.keys(positions)

const selected = computed({
get: (): string | string[] => {
if (Array.isArray(props.position)) {
return props.position.map(key => functionToKey.get(key)!)
return props.position.map(position => position.name)
}

return functionToKey.get(props.position)!
return props.position.name
},
set: (value: string | string[]): void => {
if (typeof value === 'string') {
emit('update:position', keyToFunction.get(value)!)
emit('update:position', positions[value as PositionsKey])
} else {
// eslint-disable-next-line id-length
const sorted = [...value].sort((a, b) => options.indexOf(a) - options.indexOf(b))
const selected = sorted.map(key => keyToFunction.get(key)!)
const selected = sorted.map(key => positions[key as PositionsKey])

emit('update:position', selected)
}
Expand Down
13 changes: 8 additions & 5 deletions demo/sections/components/PopOver.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</div>
<template v-if="showManual">
<div ref="manualContent" class="bg-primary text-white p-2" :style="manualStyles">
<span>Manual Composition Content</span>
<span>Manual <br> Composition Content</span>
</div>
</template>
</div>
Expand Down Expand Up @@ -75,7 +75,7 @@
</p-heading>
<div class="flex gap-2 items-center flex-wrap">
<template v-for="i in 5" :key="i">
<p-pop-over :group="group" :placement="top">
<p-pop-over :group="group" :placement="positionMethods.top">
<template #target="{ open }">
<p-button @mouseover="open">
Open {{ i }}
Expand All @@ -98,12 +98,15 @@
<script lang="ts" setup>
import { usePositionStyles, useMostVisiblePositionStyles } from '@/compositions/position'
import { usePopOverGroup } from '@/compositions/usePopOverGroup'
import { left, right, top } from '@/utilities/position'

/* eslint import/namespace: ['error', { allowComputed: true }] */
import * as positionMethods from '@/utilities/position'

import { ref } from 'vue'
import ComponentPage from '@/demo/components/ComponentPage.vue'
import PositionSelect from '@/demo/components/PositionSelect.vue'

const position = ref(left)
const position = ref(positionMethods.leftTop)
const showManual = ref(false)
const {
target: manualTarget,
Expand All @@ -112,7 +115,7 @@
styles: manualStyles,
} = usePositionStyles(position)

const positions = ref([left, right])
const positions = ref([positionMethods.left, positionMethods.right])
const showDynamic = ref(false)
const {
target: dynamicTarget,
Expand Down
40 changes: 40 additions & 0 deletions src/utilities/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,46 @@ export const bottomLeft: PositionMethod = function(target: DOMRect, content: DOM
const top = target.top - container.top + target.height
const left = target.right - container.left - target.width

return {
top,
left,
}
}

export const leftBottom: PositionMethod = function(target: DOMRect, content: DOMRect, container: DOMRect): Position {
const top = target.bottom - container.top - content.height
const left = target.left - container.left - content.width

return {
top,
left,
}
}

export const rightBottom: PositionMethod = function(target: DOMRect, content: DOMRect, container: DOMRect): Position {
const top = target.bottom - container.top - content.height
const left = target.left - container.left + target.width

return {
top,
left,
}
}

export const leftTop: PositionMethod = function(target: DOMRect, content: DOMRect, container: DOMRect): Position {
const top = target.top - container.top
const left = target.left - container.left - content.width

return {
top,
left,
}
}

export const rightTop: PositionMethod = function(target: DOMRect, content: DOMRect, container: DOMRect): Position {
const top = target.top - container.top
const left = target.left - container.left + target.width

return {
top,
left,
Expand Down
Loading