Skip to content

Commit bffe055

Browse files
authored
Better error messages for useSteps and stepper components (#1240)
* feat: provide better error messages when useSteps or stepper components are used in the wrong context * docs: describe proper useSteps usage in the docs as well
1 parent 2fd15b3 commit bffe055

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

.changeset/serious-seals-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'spectacle': minor
3+
---
4+
5+
provide better error messages when useSteps or stepper components are used in the wrong context

docs/api-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ These tags are for adding tables with content to your slides.
9090

9191
## useSteps
9292

93-
The `useSteps` hook allows a component to participate in the _slide step sequence_ for a given Slide.
93+
The `useSteps` hook allows a component to participate in the _slide step sequence_ for a given Slide. It must be called inside a component that sits somewhere underneath a slide component, i.e., `<Slide><MyComponentThatUsesUseStepsInside /></Slide>`, so it can access the `SlideContext` managed by the Slide component.
9494

9595
NOTE: the vast majority of use cases are covered by the `Stepper` and `Appear` components documented below- in fact, they are implemented via this hook. The only case in which you may need to use this hook explicitly is if you need more precise control over a component in your presentation.
9696

packages/spectacle/src/components/appear.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ const SteppedComponent = (props: SteppedComponentProps): JSX.Element => {
1616
activeStyle = { opacity: '1' },
1717
inactiveStyle = { opacity: '0' }
1818
} = props;
19-
const { immediate } = useContext(SlideContext);
19+
const slideContext = useContext(SlideContext);
20+
if (slideContext === null) {
21+
throw new Error(
22+
'This component must be used within a SlideContext.Provider. Did you' +
23+
' put an <Appear> or <Stepper> component outside of a <Slide>?'
24+
);
25+
}
26+
27+
const { immediate } = slideContext;
2028

2129
const { isActive, step, placeholder } = useSteps(numSteps, {
2230
id,

packages/spectacle/src/hooks/use-steps.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ export function useSteps(
2222
) {
2323
const [stepId] = useState(userProvidedId || ulid);
2424

25-
const { activeStepIndex, activationThresholds } = useContext(SlideContext);
25+
const slideContext = useContext(SlideContext);
26+
if (slideContext === null) {
27+
throw new Error(
28+
'`useSteps` must be called within a SlideContext.Provider. Did you' +
29+
' call `useSteps` in a component that was not placed inside a <Slide>?'
30+
);
31+
}
32+
33+
const { activeStepIndex, activationThresholds } = slideContext;
2634

2735
let relStep: number;
2836

0 commit comments

Comments
 (0)