Skip to content

Commit

Permalink
feat(plugin-rtl): provide composables
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Sep 29, 2024
1 parent ef2c444 commit 3d0e4a1
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 43 deletions.
2 changes: 2 additions & 0 deletions plugins/development/plugin-rtl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"type": "module",
"exports": {
".": "./lib/node/index.js",
"./client": "./lib/client/index.js",
"./package.json": "./package.json"
},
"main": "./lib/node/index.js",
Expand All @@ -38,6 +39,7 @@
"clean": "rimraf --glob ./lib ./*.tsbuildinfo"
},
"dependencies": {
"@vuepress/helper": "workspace:*",
"vue": "^3.5.10"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useRtl.js'
51 changes: 51 additions & 0 deletions plugins/development/plugin-rtl/src/client/composables/useRtl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { entries } from '@vuepress/helper'
import { onMounted, watch } from 'vue'
import { useRouteLocale } from 'vuepress/client'
import { getElement } from '../utils/index.js'

/**
*
* @param rtlLocalePaths rtl locale paths
* @param selectorOptions rtl selector options
*/
export const useRtl = (
rtlLocalePaths: string[],
selectorOptions?: Record<string, Record<string, string>>,
): void => {
const routeLocale = useRouteLocale()

const toggleRTL = (localePath: string): void => {
if (rtlLocalePaths.includes(localePath)) {
entries(
selectorOptions ?? {
html: { dir: 'rtl' },
},
).forEach(([selector, attrs = {}]) => {
const element = getElement(selector)

if (element)
entries(attrs).forEach(([attr, value]) => {
if (attr === 'class') element.classList.add(value)
else element.setAttribute(attr, value)
})
})
document.documentElement.style.setProperty('direction', 'rtl')
} else {
entries(rtlLocalePaths).forEach(([selector, attrs = {}]) => {
const element = getElement(selector)

if (element)
entries(attrs).forEach(([attr, value]) => {
if (attr === 'class') element.classList.remove(value)
else element.removeAttribute(attr)
})
})

document.documentElement.style.removeProperty('direction')
}
}

onMounted(() => {
watch(routeLocale, toggleRTL, { immediate: true })
})
}
46 changes: 3 additions & 43 deletions plugins/development/plugin-rtl/src/client/config.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,11 @@
import { onMounted, watch } from 'vue'
import { defineClientConfig, useRouteLocale } from 'vuepress/client'
import { defineClientConfig } from 'vuepress/client'
import { useRtl } from './composables/index.js'

declare const __RTL_LOCALES__: string[]
declare const __RTL_SELECTOR__: Record<string, Record<string, string>>

const { entries } = Object

const getElement = (selector: string): HTMLElement | null =>
selector === 'html'
? document.documentElement
: selector === 'body'
? document.body
: document.querySelector(selector)

export default defineClientConfig({
setup() {
const routeLocale = useRouteLocale()

const toggleRTL = (localePath: string): void => {
if (__RTL_LOCALES__.includes(localePath)) {
entries(__RTL_SELECTOR__).forEach(([selector, attrs = {}]) => {
const element = getElement(selector)

if (element)
entries(attrs).forEach(([attr, value]) => {
if (attr === 'class') element.classList.add(value)
else element.setAttribute(attr, value)
})
})
document.documentElement.style.setProperty('direction', 'rtl')
} else {
entries(__RTL_LOCALES__).forEach(([selector, attrs = {}]) => {
const element = getElement(selector)

if (element)
entries(attrs).forEach(([attr, value]) => {
if (attr === 'class') element.classList.remove(value)
else element.removeAttribute(attr)
})
})

document.documentElement.style.removeProperty('direction')
}
}

onMounted(() => {
watch(routeLocale, toggleRTL, { immediate: true })
})
useRtl(__RTL_LOCALES__, __RTL_SELECTOR__)
},
})
6 changes: 6 additions & 0 deletions plugins/development/plugin-rtl/src/client/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const getElement = (selector: string): HTMLElement | null =>
selector === 'html'
? document.documentElement
: selector === 'body'
? document.body
: document.querySelector(selector)
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d0e4a1

Please sign in to comment.