Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add pending logic to NavLink #8875

Merged
merged 3 commits into from
May 16, 2022

Conversation

brophdawg11
Copy link
Contributor

Adds isPending logic to NavLink for styling links during navigations. This also adds the internal refactor for isActive discussed in #8567 (comment)

let path = useResolvedPath(to);
let match = useMatch({ path: path.pathname, end, caseSensitive });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjackson Do you foresee any issue switching the internal isActive implementation to use the implementation suggested in the example for folks who want a custom implementation? All tests still pass - but I wasn't sure if there was a historical reason for the manual calculation internally.

Comment on lines +422 to +431
let nextMatch = React.useMemo(
() =>
nextLocation
? matchPath(
{ path: path.pathname, end, caseSensitive },
nextPath.pathname
)
: null,
[nextLocation, path.pathname, caseSensitive, end, nextPath.pathname]
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculate whether this matches the "next" location in the same way useMatch does internally, just using navigation.location instead of useLocation()

let match = useMatch({ path: path.pathname, end, caseSensitive });

let routerState = React.useContext(UNSAFE_DataRouterStateContext);
let nextLocation = routerState?.navigation.location;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Total side note, but we should make a hook for accessicing DataRouterStateContext that uses an invariant to warn if there is no context (they rendered the hook outside of a data router) and then return a type w/o the | null junk so we don't need these checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, now that I go looking, I actually have a private one of those used inside the other hooks that I can grab.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, scratch that. We're doing what you're getting at in our data-router-only hooks (useLoaderData etc.). This is a slightly different use-case since NavLink is allowed to be used outside a DataRouter, and when it is - isPending is just always false. So we don't want an invariant if the context doesn't exist here

@ryanflorence ryanflorence merged commit 998ca53 into feat/remixing-router May 16, 2022
@ryanflorence ryanflorence deleted the brophdawg11/navlink-pending branch May 16, 2022 20:21
ryanflorence pushed a commit that referenced this pull request May 19, 2022
* feat: add pending logic to NavLink

* chore: fixup type imports

* chore: add useMemo to nextMatch calculation
brophdawg11 added a commit that referenced this pull request Jun 6, 2022
* feat: add history package and createMemoryHistory implementation (#8702)

* feat: add initial empty history package

* feat: add createMemoryHistory + tests

* chore: add rollup buld for history

* chore: change state type from unknown -> any

* feat: Change listen interface to be pop only

* chore: fix lint warning

* Add history package to yarn workspace

* chore: Address PR feedback

* feat: Add remix-router package and loader functionality

Additional notes:
 - adds unique route id generation to react-router
 - enhances test harness to manage at navigation level
 - combines error/catch into a single exception

* feat: add support for submissions

* chore: code organization + cleanup

* ci: copy over unit test from transition manager

* fix: Fix action arguments bug

* feat: add createBrowserHistory and createHashHistory

* chore: rename package to @remix-run/router

* wip: initial router integration with React

* wip: add userLoaderData

* Remove history package and move history.ts into router

* fix: fix URLSearchParams keys typescript error

This one was tricky:
 - react-router-native used to find the type for URLSearchParams from @types/react-native
 - The new history work added @types/jsdom
 - react-router-native now finds that URLSearchParams before the @types/react-native one
 - the jsdom one imports from the internal built-in TS DOM lib
 - but it does not include the DOM.Iterable values such as keys

See: microsoft/TypeScript#38139

* feat: add data loading/rendering APIs

 - loader/action
 - useLoaderData/useActionData/useTransition
 - exceptionElement/useRouteException
 - fallbackElement

* feat: add browser/hash routers and bring over useSubmit

* feat: bring <Form> to react router-dom

* feat: support redirects returned from loaders

* feat: Support basename in data routers

* chore: remove backup _components file

* chore: extract dom.ts utils file

* feat: add <Route shouldReload> and fix bug in shouldReload logic

* chore: convert to router.subscribe for easier useSyncExternalStore usage

* chore: switch to babel-jest TS transform instead of ts-jest

* fix: make route id optional for backwards compatibility

* feat: default fallbackElement/exceptionElement + add useMacthes/useRouteData

* fix: Fix up build issues with v5-compat

* chore: renames and feedback cleanup

* feat: handle render errors through exceptionElement

* feat: move initial data load into router

* chore: do not handle exceptions for non-data-routers

* chore: remove historyv5 dependency

* feat: add support for router.revalidate()

* feat: add useRevalidator() hook

* fix: fix shouldReload support for revalidate()

* fix: handle 405 action responses correctly

This matches the behavior added to Remix in remix-run/remix#2342

* feat: handle X-Remix-Revalidate hesders returned from loader redirects

This ports over the remix behavior from remix-run/remix#2370

* chore: PR feedback

* chore: copy transition fetcher tests verbatim

* feat: add support for fetchers

* feat: add useFetcher

* ci: add tests for useFetcher/useFetchers

* chore: add formAction to submission

* chore: remove basename from data routers

* chore: change shouldReload url type

* chore: change default fallback element

* chore: add tests for cleanup and make Router type explicit

* feat: support returning responses from loaders/actions

automatically parses the response body with json() or text() depending on the content type of the response. This makes it really convenient to use `fetch` and brings the server semantics to client loaders like `json(data)` or `new Response(“”, { status: 404 })` etc.

* chore: cleanup auto-unwrap logic, add action info to request, remove action submission fields

* feat: provide full control to users in shouldRevalidate

* chore: remove hashchange and create createUrlBasedHistory abstraction

* fix: handle syncronous initial load

* fix request patch

* feat: support fetcher opt-in revalidation

* ci: clean up revalidating fetcher tests and logic

* feat: wiure up useFetcher and turn off revalidation on submit

* docs: update transition/fetcher diagrams

* ci: bump bundle thresholds for new data routing

* chore: update to latest @web-std/fetch with proper formData handling

Our PR was merged and released in 4.1.0 so we can remove our local override.
See: web-std/io#60

* feat: move to better cross-browser fallback spinner

* Revert "feat: move to better cross-browser fallback spinner"

This reverts commit a887019.

* chore: Rename "exception" to "error"

Done in steps via grep/sed:
 - exceptionElement -> errorElement
 - useRouteException -> useRouteError
 - Exception -> Error
 - exceptions -> errors
 - exception -> error

* feat: handle revalidation for interrupted submissions

feat: change defaultShouldRevalidate from funciton -> boolean
chore: simplify revalidation via isRevalidationRequired

* feat: opt-in fetcher.load to revalidation by default

* feat: send actionResult to shouldRevalidate

* feat: add <ScrollRestoration> component and <Link resetScroll=false>

* chore: rename navigation -> transition

* feat: flatten submission onto shouldRevalidate

* feat: simplify form submit logic via event.submitter

Copies the changes from remix remix-run/remix#3094

* chore: clean up types

* chore: sync up react-router-native public api

* feat: properly support React 18 + React.StrictMode

* chore: fix lint warnings

* chore: remove default fallback element

* chore: add @remix-run/router to example vite configs for USE_SOURCE

* chore: add data-router and scroll-restoration examples

* chore: switch from @web-std/fetch to @remix-run/web-fetch for tests

* feat: add pending logic to NavLink (#8875)

* feat: add pending logic to NavLink

* chore: fixup type imports

* chore: add useMemo to nextMatch calculation

* chore: inline react use-sync-external-store/shim for UMD builds

* chore: clean up some exports and add some jsdocs

* feat: add DataStaticRouter for SSR

* chore: fix link

* fix: default replace=false only for GET submissions

* chore: update scroll restoration example to include data loading

* feat: remove fetcher.type since it can be derived

* feat: Remove promise from return signature of router methods

* chore: remove encType from Form props

* WIP docs

* feat: auto-unwrap error Responses into ErrorResponse

* chore: add error boundary example

* feat: support routes prop on data routers

* docs: moar docs

* chore: remove navigation.type

* docs docs docs

* dooooooooooooooooooocs

* docs: fix contributing link (#8885)

* docs: fix contributing link

* chore: add alexlbr to contributors

* Docs: Fix typo in getting-started > concepts (#8888)

* docs: fix small typo in getting-started > concepts

* Sign CLA: add tanayv to contributors.yml

* chore: sort contributors list

* docs: think it’s ready for the big time

* docs: notes demo

* Update overview.md (#8892)

* Update overview.md

* Update contributors.yml

* Version 6.4.0-pre.0

* docs: link fixes

* chore: publish @remix-run/router

* Version 6.4.0-pre.1

* chore: publish @remix-run/router

* Version 6.4.0-pre.2

* docs: DataStaticRouter stub

* docs: not sure what this was 😂

* docs: link to the demo

* fix: properly handle 404s using error boundaries

* docs: getting started installation

* Convert formMethod=GET to be a loading navigation instead of submitting

* Handle invalid GET formData via 400 Bad Request error

* fix: Properly handle descendent routes within a data router

* docs: update useMatches and useNavigation docs

* fix: better solution for 404 error boundaries

* chore: refactor resetScroll to avoid history state mutation

* docs: add note on useMatches and descendent routes

* chore: move newly added resolveTo test from react-router to router

* ci: Remove website workflow before merging remixing work into dev

Co-authored-by: Ryan Florence <rpflorence@gmail.com>
Co-authored-by: Alex Lobera <alex.lobera@gmail.com>
Co-authored-by: Tanay Vardhan <tvardha2@illinois.edu>
Co-authored-by: remix-cla-bot[bot] <92060565+remix-cla-bot[bot]@users.noreply.github.com>
Co-authored-by: Michal Petrik <misopetrik@gmail.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants