-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathDirection.ts
40 lines (32 loc) · 1.06 KB
/
Direction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { computed, defineComponent, inject, provide, toRefs } from 'vue'
import type { InjectionKey, PropType, Ref } from 'vue'
type Direction = 'ltr' | 'rtl'
const DirectionContextSymbol = Symbol('OkuDirectionProvider') as InjectionKey<Ref<Direction>>
export interface DirectionProviderProps {
dir: Direction
}
export const directionProviderProps = {
props: {
dir: {
type: String as PropType<Direction>,
required: true,
},
},
}
const DirectionProvider = defineComponent({
name: 'DirectionProvider',
props: {
...directionProviderProps.props,
},
setup(props, { slots }) {
const { dir } = toRefs(props)
provide(DirectionContextSymbol, dir as Ref<Direction>)
return () => slots.default?.()
},
})
/* ----------------------------------------------------------------------------------------------- */
export function useDirection(localDir?: Direction) {
const globalDir = inject(DirectionContextSymbol, null)
return computed(() => localDir ?? globalDir?.value ?? 'ltr')
}
export const OkuDirectionProvider = DirectionProvider