Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-seals-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'spectacle': minor
---

provide better error messages when useSteps or stepper components are used in the wrong context
2 changes: 1 addition & 1 deletion docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ These tags are for adding tables with content to your slides.

## useSteps

The `useSteps` hook allows a component to participate in the _slide step sequence_ for a given Slide.
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.

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.

Expand Down
10 changes: 9 additions & 1 deletion packages/spectacle/src/components/appear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ const SteppedComponent = (props: SteppedComponentProps): JSX.Element => {
activeStyle = { opacity: '1' },
inactiveStyle = { opacity: '0' }
} = props;
const { immediate } = useContext(SlideContext);
const slideContext = useContext(SlideContext);
if (slideContext === null) {
throw new Error(
'This component must be used within a SlideContext.Provider. Did you' +
' put an <Appear> or <Stepper> component outside of a <Slide>?'
);
}

const { immediate } = slideContext;

const { isActive, step, placeholder } = useSteps(numSteps, {
id,
Expand Down
10 changes: 9 additions & 1 deletion packages/spectacle/src/hooks/use-steps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ export function useSteps(
) {
const [stepId] = useState(userProvidedId || ulid);

const { activeStepIndex, activationThresholds } = useContext(SlideContext);
const slideContext = useContext(SlideContext);
if (slideContext === null) {
throw new Error(
'`useSteps` must be called within a SlideContext.Provider. Did you' +
' call `useSteps` in a component that was not placed inside a <Slide>?'
);
}

const { activeStepIndex, activationThresholds } = slideContext;

let relStep: number;

Expand Down