Skip to content

Commit 997154b

Browse files
authored
[Flare] Add FocusWithin responder (#16152)
FocusWithin is implemented as a separate responder, which keeps both focus responders simple and allows for easier composition of behaviours.
1 parent 65b80fd commit 997154b

File tree

3 files changed

+676
-173
lines changed

3 files changed

+676
-173
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# FocusWithin
2+
3+
The `FocusWithin` module responds to focus and blur events on its child. Focus events
4+
are dispatched for all input types, with the exception of `onFocusVisibleChange`
5+
which is only dispatched when focusing with a keyboard.
6+
7+
Focus events do not propagate between `FocusWithin` event responders.
8+
9+
```js
10+
// Example
11+
const Button = (props) => {
12+
const [ focusWithin, updateFocusWithin ] = useState(false);
13+
const [ focusWithinVisible, updateFocusWithinVisible ] = useState(false);
14+
15+
return (
16+
<FocusWithin
17+
onFocusWithinChange={updateFocusWithin}
18+
onFocusWithinVisibleChange={updateFocusWithinVisible}
19+
>
20+
<button
21+
children={props.children}
22+
style={{
23+
...(focusWithin && focusWithinStyles),
24+
...(focusWithinVisible && focusWithinVisibleStyles)
25+
}}
26+
>
27+
</FocusWithin>
28+
);
29+
};
30+
```
31+
32+
## Types
33+
34+
```js
35+
type FocusEvent = {
36+
target: Element,
37+
type: 'focuswithinchange' | 'focuswithinvisiblechange'
38+
}
39+
```
40+
41+
## Props
42+
43+
### disabled: boolean = false
44+
45+
Disables all `FocusWithin` events.
46+
47+
### onFocusWithinChange: boolean => void
48+
49+
Called once the element or a descendant receives focus, and once focus moves
50+
outside of the element.
51+
52+
### onFocusWithinVisibleChange: boolean => void
53+
54+
Called once the element or a descendant is focused following keyboard
55+
navigation, and once focus moves outside of the element. This can be used to
56+
display focus styles only when the keyboard is being used to focus within the
57+
element's subtree.

0 commit comments

Comments
 (0)