Skip to content

Commit

Permalink
refactor(CPopover, CTabs, CTooltips): change to an SSR-friendly uniqu…
Browse files Browse the repository at this point in the history
…e ID generator
  • Loading branch information
mrholek committed May 30, 2024
1 parent a2767fb commit 78f7c60
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 21 deletions.
7 changes: 4 additions & 3 deletions packages/coreui-vue/src/components/popover/CPopover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { defineComponent, h, onMounted, PropType, ref, RendererElement, Transiti
import type { Placement } from '@popperjs/core'

import { CConditionalTeleport } from '../conditional-teleport'
import { usePopper } from '../../composables'
import { useUniqueId, usePopper } from '../../composables'
import type { Placements, Triggers } from '../../types'
import { executeAfterTransition } from '../../utils/transition'
import { getRTLPlacement, getUID } from '../../utils'
import { getRTLPlacement } from '../../utils'

const CPopover = defineComponent({
name: 'CPopover',
Expand Down Expand Up @@ -119,6 +119,7 @@ const CPopover = defineComponent({
const popoverRef = ref()
const uID = ref()
const visible = ref(props.visible)
const { getUID } = useUniqueId('popover')
const { initPopper, destroyPopper } = usePopper()

const delay =
Expand Down Expand Up @@ -149,7 +150,7 @@ const CPopover = defineComponent({
}

onMounted(() => {
uID.value = getUID('popover')
uID.value = getUID()
})

const handleEnter = (el: RendererElement, done: () => void) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/coreui-vue/src/components/tabs/CTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const CTab = defineComponent({
active: isActive(),
},
],
id: `${id.value}${props.itemKey}-tab`,
id: `${props.itemKey}-tab-${id.value}`,
role: 'tab',
tabindex: isActive() ? 0 : -1,
type: 'button',
'aria-controls': `${id.value}${props.itemKey}-tab-pane`,
'aria-controls': `${props.itemKey}-tab-panel-${id.value}`,
'aria-selected': isActive(),
onClick: () => setActiveItemKey(props.itemKey),
onFocus: () => setActiveItemKey(props.itemKey),
Expand Down
4 changes: 2 additions & 2 deletions packages/coreui-vue/src/components/tabs/CTabPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ const CTabPanel = defineComponent({
show: firstRender.value && visible.value,
},
],
id: `${id.value}${props.itemKey}-tab-pane`,
id: `${props.itemKey}-tab-panel-${id.value}`,
role: 'tabpanel',
'aria-labelledby': `${id.value}${props.itemKey}-tab`,
'aria-labelledby': `${props.itemKey}-tab-${id.value}`,
tabindex: 0,
ref: tabPaneRef,
},
Expand Down
5 changes: 3 additions & 2 deletions packages/coreui-vue/src/components/tabs/CTabs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineComponent, h, provide, ref, watch } from 'vue'
import { getUID } from '../../utils'
import { useUniqueId } from '../../composables'

const CTabs = defineComponent({
name: 'CTabs',
Expand All @@ -19,7 +19,8 @@ const CTabs = defineComponent({
'change',
],
setup(props, { slots, emit }) {
const uID = ref(getUID('t'))
const { getUID } = useUniqueId()
const uID = ref(getUID())
const activeItemKey = ref(props.activeItemKey)
const setActiveItemKey = (key: string | number) => {
activeItemKey.value = key
Expand Down
7 changes: 4 additions & 3 deletions packages/coreui-vue/src/components/tooltip/CTooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { defineComponent, h, onMounted, PropType, ref, RendererElement, Transiti
import type { Placement } from '@popperjs/core'

import { CConditionalTeleport } from '../conditional-teleport'
import { usePopper } from '../../composables'
import { useUniqueId, usePopper } from '../../composables'
import type { Placements, Triggers } from '../../types'
import { executeAfterTransition } from '../../utils/transition'
import { getRTLPlacement, getUID } from '../../utils'
import { getRTLPlacement } from '../../utils'

const CTooltip = defineComponent({
name: 'CTooltip',
Expand Down Expand Up @@ -115,6 +115,7 @@ const CTooltip = defineComponent({
const tooltipRef = ref()
const uID = ref()
const visible = ref(props.visible)
const { getUID } = useUniqueId('popover')
const { initPopper, destroyPopper } = usePopper()

const delay =
Expand Down Expand Up @@ -145,7 +146,7 @@ const CTooltip = defineComponent({
}

onMounted(() => {
uID.value = getUID('tooltip')
uID.value = getUID()
})

const handleEnter = (el: RendererElement, done: () => void) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/coreui-vue/src/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useColorModes } from './useColorModes'
import { useUniqueId } from './useUniqueId'
import { usePopper } from './usePopper'

export { useColorModes, usePopper }
export { useColorModes, useUniqueId, usePopper }
18 changes: 18 additions & 0 deletions packages/coreui-vue/src/composables/useUniqueId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ref } from 'vue'

export const useUniqueId = (prefix: string = '') => {
const ids = ref<string[]>([])

const getUID = () => {
do {
prefix += Math.floor(Math.random() * 1_000_000)
} while (ids.value.includes(prefix))

ids.value.push(prefix)
return prefix
}

return {
getUID,
}
}
6 changes: 3 additions & 3 deletions packages/coreui-vue/src/directives/v-c-popover.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { DirectiveBinding } from 'vue'
import { createPopper } from '@popperjs/core'

import type { Options } from '@popperjs/core'

import { getUID } from '../utils'
import { useUniqueId } from '../composables'

const createPopoverElement = (id: string, header: string, content: string): HTMLDivElement => {
const popover = document.createElement('div')
Expand Down Expand Up @@ -56,6 +55,7 @@ export default {
name: 'c-popover',
uid: '',
mounted(el: HTMLElement, binding: DirectiveBinding): void {
const { getUID } = useUniqueId('popover')
const value = binding.value
const content = typeof value === 'string' ? value : value.content ?? ''
const header = value.header ?? ''
Expand All @@ -77,7 +77,7 @@ export default {
],
}

const uID = getUID('popover')
const uID = getUID()
binding.arg = uID
const popover = createPopoverElement(uID, header, content)

Expand Down
6 changes: 3 additions & 3 deletions packages/coreui-vue/src/directives/v-c-tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { DirectiveBinding } from 'vue'
import { createPopper } from '@popperjs/core'

import type { Options } from '@popperjs/core'

import { getUID } from '../utils'
import { useUniqueId } from '../composables'

const createTooltipElement = (id: string, content: string): HTMLDivElement => {
const tooltip = document.createElement('div')
Expand Down Expand Up @@ -54,6 +53,7 @@ const toggleTooltipElement = (
export default {
name: 'c-tooltip',
mounted(el: HTMLElement, binding: DirectiveBinding): void {
const { getUID } = useUniqueId('tooltip')
const value = binding.value
const content = typeof value === 'string' ? value : value.content ?? ''
const trigger = value.trigger ?? 'hover'
Expand All @@ -74,7 +74,7 @@ export default {
],
}

const uID = getUID('tooltip')
const uID = getUID()
binding.arg = uID
const tooltip = createTooltipElement(uID, content)

Expand Down
2 changes: 0 additions & 2 deletions packages/docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { defineUserConfig } from 'vuepress'
import { viteBundler } from '@vuepress/bundler-vite'
import { activeHeaderLinksPlugin } from '@vuepress/plugin-active-header-links'
import { backToTopPlugin } from '@vuepress/plugin-back-to-top'
import { markdownContainerPlugin } from '@vuepress/plugin-markdown-container'
import { prismjsPlugin } from '@vuepress/plugin-prismjs'
import { registerComponentsPlugin } from '@vuepress/plugin-register-components'
Expand All @@ -11,7 +10,6 @@ import anchor from 'markdown-it-anchor'
import include_plugin from 'markdown-it-include'
import { defaultTheme } from './src/node'

import { fileURLToPath, URL } from 'url'
const __dirname = getDirname(import.meta.url)

export default defineUserConfig({
Expand Down

0 comments on commit 78f7c60

Please sign in to comment.