Skip to content

Commit

Permalink
feat: add skipInitialTransition to make skipping opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 29, 2020
1 parent 0871617 commit f200ad0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ Fired on: `CLOSE`.
The `yin` to `onSpringStart`'s `yang`. It has the same characteristics. Including `async/await` and Promise support for delaying a transition. For `CLOSE` it gives you a hook into the step right after it has cleaned up everything after itself, and right before it unmounts itself. This can be useful if you have some logic that needs to perform some work before it's safe to unmount.
### skipInitialTransition
Type: `boolean`
By default the initial open state is always transitioned to using an spring animation. Set `skipInitialTransition` to `true` and the initial `open` state will render as if it were the default state. Useful to avoid scenarios where the opening transition would be distracting.
## ref
Methods available when setting a `ref` on the sheet:
Expand Down
1 change: 1 addition & 0 deletions pages/fixtures/scrollable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const ScrollableFixturePage: NextPage<GetStaticProps> = ({
<SnapMarker style={{ top: '75vh', ['--size' as any]: '0.5vh' }} />
<BottomSheet
open
skipInitialTransition
sibling={<CloseExample className="z-10" />}
ref={sheetRef}
initialFocusRef={focusRef}
Expand Down
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type { RefHandles as BottomSheetRef } from './types'

// Because SSR is annoying to deal with, and all the million complaints about window, navigator and dom elenents!
export const BottomSheet = forwardRef<RefHandles, Props>(function BottomSheet(
{ onSpringStart, onSpringEnd, ...props },
{ onSpringStart, onSpringEnd, skipInitialTransition, ...props },
ref
) {
// Mounted state, helps SSR but also ensures you can't tab into the sheet while it's closed, or nav there in a screen reader
Expand All @@ -23,7 +23,7 @@ export const BottomSheet = forwardRef<RefHandles, Props>(function BottomSheet(
// It's only when initialState and props.open is mismatching that a intial transition should happen
// If they match then transitions will only happen when a user interaction or resize event happen.
const initialStateRef = useRef<'OPEN' | 'CLOSED'>(
props.open ? 'OPEN' : 'CLOSED'
skipInitialTransition && props.open ? 'OPEN' : 'CLOSED'
)

// Using layout effect to support cases where the bottom sheet have to appear already open, no transition
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export type Props = {

/* Configures body-scroll-lock to reserve scrollbar gap by setting padding on <body>, clears when closing the bottom sheet. on by default iff blocking=true */
reserveScrollBarGap?: boolean

/* Open immediatly instead of initially animating from a closed => open state, useful if the bottom sheet is visible by default and the animation would be distracting */
skipInitialTransition?: boolean
} & Omit<React.PropsWithoutRef<JSX.IntrinsicElements['div']>, 'children'>

export interface RefHandles {
Expand Down

0 comments on commit f200ad0

Please sign in to comment.