`
36 |
37 | import SelfFocus from 'react-selffocus-element';
38 |
39 | ...
40 | render(){
41 | return (
42 |
43 | This will only be content that will be focused on component mount.
44 |
45 | )
46 | }
47 | ...
48 |
49 | This is render `div`(by default) tag with autofocus. This element will also be focus-able by default.
50 |
51 | `Rendered DOM`
52 |
53 | This will only be content that will be focused on component mount.
54 |
55 | 2. With Custom Tag and TabIndex
56 |
57 |
58 | import SelfFocus from 'react-selffocus-element';
59 |
60 | ...
61 | render(){
62 | return (
63 |
64 | This will only be content for custom tag and will be focused on component mount.
65 |
66 | )
67 | }
68 | ...
69 |
70 | This is render `p`(`tag` prop) tag with autofocus. This element will be focus-able based on tabIndex prop. It is recommended that value of this prop should be 0 (natural tab order) or -1 (not tabbable).
71 |
72 | `Rendered DOM `
73 |
74 | This will only be content for custom tag and will be focused on component mount.
75 |
76 | ## APIs
77 |
78 | ### `SelfFocus` Component
79 |
80 | #### Import mechanism
81 |
82 | import SelfFocus from 'react-selffocus-element'
83 |
84 | #### Properties
85 |
86 | | prop | type | description | default value |
87 | | ------------------ | --------------- | ------------------------------------------- | ------------- |
88 | | children (default) | -- | Inner children for selfFocus Component | `null` |
89 | | tag | htmlTag(String) | Component/Node to be rendered for focussing | `div` |
90 | | className | string | additional Classname for particular div | `` |
91 | | tabIndex | string/number | tabbable order - 0/-1 | `0` |
92 |
93 | ## FAQ
94 |
95 | #### 1. I do not see focused element with outline. How can it be controlled?
96 |
97 | One should use additional custom css to achieve outline, which is normally in this form,
98 |
99 | *:focus {
100 | outline-style: auto !important;
101 | outline: auto !important;
102 | outline-color: #2793f8 !important;
103 | }
104 |
105 | Also note that outline behavior for screen reader will also rely on screen reader and browser ( for eg, on electron running on window will be default render yellow border unless overwritten by css)
106 |
107 | #### 2. Should I use it for form input tag?
108 |
109 | This component can be used for input tags but default `autoFocus` prop support provided by React should be used in conjunction with input tags. This will help browser functionalities to work as per focus specifications.
110 |
111 | #### 3. What about `role` and `aria-\*` attributes for that elements
112 |
113 | You can specify `role` and all `aria-*` attributes on SelfFocus component and would be available on parent element.
114 |
115 | e.g.
116 |
117 |
118 |
119 | This will render a `p` tag with `role` as `alert`
120 |
121 | #### 4. What about other props that my component requires?
122 |
123 | You can pass any key-value prop to `SelfFocus` and it will be rendered on main parent element. This is also how `aria-*` and `role` is supported.
124 |
125 | #### 5. Does this work on ComponentDidUpdate?
126 |
127 | No. There is no use case of focusing again on element after some state/prop change. In addition, there may be `componentDidUpdate` function triggered when it does not require focusing. Hence, it is currently not supported.
128 |
129 | ## License
130 |
131 | [](LICENSE)
132 |
133 | Refer `LICENSE` file in this repository.
134 |
--------------------------------------------------------------------------------