-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathRovingFocusGroupImpl.ts
180 lines (162 loc) · 6.19 KB
/
RovingFocusGroupImpl.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import type { ComputedRef, PropType } from 'vue'
import { computed, defineComponent, h, mergeProps, reactive, ref, toRefs, watchEffect } from 'vue'
import { useComposedRefs, useControllable, useForwardRef } from '@oku-ui/use-composable'
import type { OkuElement } from '@oku-ui/primitive'
import { Primitive, primitiveProps } from '@oku-ui/primitive'
import { composeEventHandlers } from '@oku-ui/utils'
import type { RovingFocusGroupOptions } from './utils'
import { focusFirst, rovingFocusGroupOptionsProps } from './utils'
import { rovingFocusProvider, useCollection } from './RovingFocusGroup'
import { scopedProps } from './types'
const ENTRY_FOCUS = 'rovingFocusGroup.onEntryFocus'
const EVENT_OPTIONS = { bubbles: false, cancelable: true }
export type RovingFocusGroupImplNaviteElement = OkuElement<'div'>
export type RovingFocusGroupImplElement = HTMLDivElement
export interface RovingFocusGroupImplProps extends RovingFocusGroupOptions {
currentTabStopId?: string | null
defaultCurrentTabStopId?: string
}
export type RovingFocusGroupImplEmits = {
currentTabStopIdChange: [tabStopId: string | null]
entryFocus: [event: Event]
mousedown: [event: MouseEvent]
focus: [event: FocusEvent]
blur: [event: FocusEvent]
}
export const rovingFocusGroupImplProps = {
props: {
currentTabStopId: String as unknown as PropType<ComputedRef<string | null>>,
defaultCurrentTabStopId: String,
...rovingFocusGroupOptionsProps.props,
},
emits: {
// eslint-disable-next-line unused-imports/no-unused-vars
entryFocus: (event: Event) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
currentTabStopIdChange: (tabStopId: string | null) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
mousedown: (event: MouseEvent) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
focus: (event: FocusEvent) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
blur: (event: FocusEvent) => true,
},
}
const RovingFocusGroupImpl = defineComponent({
name: 'OkuRovingFocusGroupImpl',
inheritAttrs: false,
props: {
...rovingFocusGroupImplProps.props,
...primitiveProps,
...scopedProps,
},
emits: rovingFocusGroupImplProps.emits,
setup(props, { attrs, slots, emit }) {
const {
scopeOkuRovingFocusGroup,
orientation,
loop,
dir,
currentTabStopId: currentTabStopIdProp,
defaultCurrentTabStopId,
...groupProps
} = toRefs(props)
const buttonRef = ref<HTMLDivElement | null>(null)
const forwardedRef = useForwardRef()
const composedRefs = useComposedRefs(buttonRef, forwardedRef)
const { state: currentTabStopId, updateValue: updateCurrentTabStopId } = useControllable({
prop: computed(() => currentTabStopIdProp.value),
defaultProp: computed(() => defaultCurrentTabStopId.value),
onChange: (result: any) => {
emit('currentTabStopIdChange', result)
},
initialValue: null,
})
const isTabbingBackOut = ref(false)
const getItems = useCollection(scopeOkuRovingFocusGroup.value)
const isClickFocusRef = ref(false)
const focusableItemsCount = ref(0)
watchEffect(() => {
const node = buttonRef.value
if (node) {
node.addEventListener(ENTRY_FOCUS, (event) => {
emit('entryFocus', event)
})
return () => node.removeEventListener(ENTRY_FOCUS, (event) => {
emit('entryFocus', event)
})
}
})
rovingFocusProvider({
scope: scopeOkuRovingFocusGroup.value,
orientation,
dir,
loop,
currentTabStopId,
onItemFocus: (tabStopId: string) => {
updateCurrentTabStopId(tabStopId)
},
onItemShiftTab: () => {
isTabbingBackOut.value = true
},
onFocusableItemAdd: () => {
focusableItemsCount.value++
},
onFocusableItemRemove: () => {
focusableItemsCount.value--
},
})
const _reactiveProupProps = reactive(groupProps)
return () => {
return h(Primitive.div, {
'tabindex': isTabbingBackOut.value || focusableItemsCount.value === 0 ? -1 : 0,
'data-orientation': orientation?.value,
...mergeProps(attrs, _reactiveProupProps),
'ref': composedRefs,
'style': {
outline: 'none',
...attrs.style as any,
},
'onMousedown': composeEventHandlers<MouseEvent>((e) => {
emit('mousedown', e)
}, () => {
isClickFocusRef.value = true
}),
'onFocus': composeEventHandlers<FocusEvent>((e) => {
emit('focus', e)
}, (event: FocusEvent) => {
// We normally wouldn't need this check, because we already check
// that the focus is on the current target and not bubbling to it.
// We do this because Safari doesn't focus buttons when clicked, and
// instead, the wrapper will get focused and not through a bubbling event.
const isKeyboardFocus = !isClickFocusRef.value
if (event.target === event.currentTarget && isKeyboardFocus && !isTabbingBackOut.value) {
const entryFocusEvent = new CustomEvent(ENTRY_FOCUS, EVENT_OPTIONS)
event.currentTarget?.dispatchEvent(entryFocusEvent)
if (!entryFocusEvent.defaultPrevented) {
const items = getItems().filter(item => item.focusable)
const activeItem = items.find(item => item.active)
const currentItem = items.find(item => item.id === currentTabStopId.value)
const candidateItems = [activeItem, currentItem, ...items].filter(
Boolean,
) as typeof items
const candidateNodes = candidateItems.map(item => item.ref.value)
focusFirst(candidateNodes)
}
}
isClickFocusRef.value = false
}),
'onBlur': composeEventHandlers<FocusEvent>((e) => {
emit('blur', e)
}, () => {
isTabbingBackOut.value = false
}),
}, slots)
}
},
})
// TODO: https://github.com/vuejs/core/pull/7444 after delete
export const OkuRovingFocusGroupImpl = RovingFocusGroupImpl as typeof RovingFocusGroupImpl &
(new () => {
$props: RovingFocusGroupImplNaviteElement
})