Skip to content

Commit

Permalink
reuse main router transition in links
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Aug 2, 2024
1 parent d9b63d3 commit 7ade36e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-deers-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rescript-relay-router": patch
---

Make sure links also reuse the same main router transition
18 changes: 8 additions & 10 deletions packages/rescript-relay-router/src/RelayRouter.res
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,12 @@ module Router = {
~pathParams=match.params,
~queryParams,
~location,
)->(
Js.Promise.then_(
assetsToPreload => {
assetsToPreload->Belt.Array.forEach(a => a->preloadAsset(~priority))
Js.Promise.resolve()
},
_,
)
)->Js.Promise.then_(
assetsToPreload => {
assetsToPreload->Belt.Array.forEach(a => a->preloadAsset(~priority))
Js.Promise.resolve()
},
_,
)
}, ~priority)
})
Expand Down Expand Up @@ -280,13 +278,13 @@ module RouteRenderer = {
</RouteComponent>
})

<>
<RelayRouter__Internal.RouterTransitionContext.Provider value=startTransition>
{switch renderPending {
| Some(renderPending) => renderPending(isPending)
| None => React.null
}}
{renderedContent.contents}
</>
</RelayRouter__Internal.RouterTransitionContext.Provider>
}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/rescript-relay-router/src/RelayRouter__Internal.res
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,15 @@ let runAtPriority = (cb, ~priority) => {
None
}
}

module RouterTransitionContext = {
type transitionFn = (unit => unit) => unit

let context = React.createContext(_cb => ())

module Provider = {
let make = React.Context.provider(context)
}

let use = (): transitionFn => React.useContext(context)
}
10 changes: 10 additions & 0 deletions packages/rescript-relay-router/src/RelayRouter__Internal.resi
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ let runAtPriority: (
RelayRouter__Types.callback,
~priority: RelayRouter__Types.preloadPriority,
) => option<unit => unit>

module RouterTransitionContext: {
type transitionFn = (unit => unit) => unit
let context: React.Context.t<transitionFn>
let use: unit => transitionFn

module Provider: {
let make: React.component<React.Context.props<transitionFn>>
}
}
2 changes: 1 addition & 1 deletion packages/rescript-relay-router/src/RelayRouter__Link.res
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ let make = (
) => {
let linkElement = React.useRef(Js.Nullable.null)
let hasPreloaded = React.useRef(false)
let (_, startTransition) = React.useTransition()
let router = RelayRouter__Context.useRouterContext()
let {history} = router
let targetElementRef = TargetScrollElement.useTargetElement()
let startTransition = RelayRouter__Internal.RouterTransitionContext.use()

let changeRoute = React.useCallback(e =>
startTransition(() => {
Expand Down

0 comments on commit 7ade36e

Please sign in to comment.