Skip to content

Commit 2e4b048

Browse files
committed
Use a ref to more reliably set focus
1 parent 299739a commit 2e4b048

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
2+
.DS_Store
23
.next
34
todo.md
45
current.md
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Slideshow with controls autofocus
2+
3+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. At imperdiet dui accumsan sit amet nulla facilities morbi tempus. Praesent elementum facilisis leo vel fringilla. Congue mauris rhoncus aenean vel. Egestas sed tempus urna et pharetra pharetra massa massa ultricies.
4+
5+
<CH.Slideshow preset="https://codesandbox.io/s/w5wfe" hasAutoFocusControls>
6+
7+
Lorem ipsum dolor sit amet, consectetur adipiscing something about points, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
8+
9+
```jsx src/App.js
10+
import { motion } from "framer-motion"
11+
12+
const transition = { duration: 1 }
13+
14+
export default function App() {
15+
const bg = "hsl(20, 100%, 50%)"
16+
return (
17+
<div className="container">
18+
<motion.div
19+
className="swatch"
20+
animate={{
21+
backgroundColor: bg,
22+
}}
23+
transition={transition}
24+
/>
25+
</div>
26+
)
27+
}
28+
```
29+
30+
---
31+
32+
## Step 2
33+
34+
Velit euismod in pellentesque massa placerat. Mi bibendum neque egestas congue quisque egestas diam in arcu. Nisi lacus sed viverra tellus in.
35+
36+
```jsx src/App.js focus=1,6,9:15
37+
import { motion } from "framer-motion"
38+
39+
const transition = { duration: 1 }
40+
41+
export default function App() {
42+
const bg = "hsl(110, 100%, 50%)"
43+
return (
44+
<div className="container">
45+
<motion.div
46+
className="swatch"
47+
animate={{
48+
backgroundColor: bg,
49+
}}
50+
transition={transition}
51+
/>
52+
</div>
53+
)
54+
}
55+
```
56+
57+
---
58+
59+
## Step 3
60+
61+
Id aliquet risus feugiat in ante metus dictum at tempor. Sed blandit libero volutpat sed cras.
62+
63+
```jsx src/App.js focus=1,6,9:15
64+
import { motion } from "framer-motion"
65+
66+
const transition = { duration: 1 }
67+
68+
export default function App() {
69+
const bg = "hsl(200, 100%, 50%)"
70+
return (
71+
<div className="container">
72+
<motion.div
73+
className="swatch"
74+
animate={{
75+
backgroundColor: bg,
76+
}}
77+
transition={transition}
78+
/>
79+
</div>
80+
)
81+
}
82+
```
83+
84+
---
85+
86+
## Step 4
87+
88+
Velit euismod in pellentesque massa placerat. Mi bibendum neque egestas congue quisque egestas diam in arcu.
89+
90+
```jsx src/App.js focus=1,6,9:15
91+
import { motion } from "framer-motion"
92+
93+
const transition = { duration: 1 }
94+
95+
export default function App() {
96+
const bg = "hsl(290, 100%, 50%)"
97+
return (
98+
<div className="container">
99+
<motion.div
100+
className="swatch"
101+
animate={{
102+
backgroundColor: bg,
103+
}}
104+
transition={transition}
105+
/>
106+
</div>
107+
)
108+
}
109+
```
110+
111+
</CH.Slideshow>

packages/mdx/src/mdx-client/slideshow.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export function Slideshow({
2626
presetConfig?: PresetConfig
2727
style?: React.CSSProperties
2828
}) {
29+
const controlsRef = React.useRef(null);
30+
31+
React.useEffect(() => {
32+
// Only set focus on controls input if we have configured to do so
33+
hasAutoFocusControls && controlsRef.current.focus();
34+
}, []);
35+
2936
const { stepsChildren, previewChildren } =
3037
extractPreviewSteps(children, hasPreviewSteps)
3138
const withPreview = presetConfig || hasPreviewSteps
@@ -100,12 +107,12 @@ export function Slideshow({
100107
Prev
101108
</button>
102109
<input
103-
autoFocus={hasAutoFocusControls}
104-
type="range"
105-
min={0}
106110
max={editorSteps.length - 1}
107-
value={state.stepIndex}
111+
min={0}
112+
ref={controlsRef}
108113
step={1}
114+
type="range"
115+
value={state.stepIndex}
109116
onChange={e =>
110117
setState({
111118
stepIndex: +e.target.value,

0 commit comments

Comments
 (0)