-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Navigator: add NavigatorButton
and NavigatorBackButton
components
#38634
Conversation
Size Change: +53 B (0%) Total Size: 1.15 MB
ℹ️ View Unchanged
|
5230935
to
596eb0d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a very good introduction for me on how to structure these hook-based, next-gen components. Thanks! 😄
packages/components/src/navigator/navigator-back-link/component.tsx
Outdated
Show resolved
Hide resolved
packages/components/src/navigator/navigator-back-link/component.tsx
Outdated
Show resolved
Hide resolved
* <NavigatorProvider initialPath="/"> | ||
* <NavigatorScreen path="/"> | ||
* <p>This is the home screen.</p> | ||
* <NavigatorLink variant="secondary" path="/child"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should avoid having this variant="secondary"
prop in all our examples, because it's a bit confusing on first glance and leaks the fact that it's internally a Button
.
I see a few possibilities here:
- If the intent is to give it
secondary
styling by default, theNavigatorLink
component should perhaps handle that internally. - If we do want to highlight that it's internally a
Button
, that should probably be made clearer in the docs. - Looking at the actual use cases in our codebase though, I think we can assume that most consumers will be using this via
as
prop or hook. Should we maybe highlight that kind of example instead? If this is the main use case, we could even simply further and make the internal component a lowercase-b<button>
.
Probably some combination of 1 and 3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the intent is to give it secondary styling by default, the NavigatorLink component should perhaps handle that internally.
This was partially the intent. But your point makes sense — I went ahead and removed variant="secondary"
from the examples in 68fac76.
Also wanted to note that we're using that same prop in the Storybook examples
Looking at the actual use cases in our codebase though, I think we can assume that most consumers will be using this via as prop or hook. Should we maybe highlight that kind of example instead? If this is the main use case, we could even simply further and make the internal component a lowercase-b .
Not not about a lot of consumers using the as
prop — I think that using Button
is a sensible default that may cover a good amount of scenarios. For more context, this was discussed originally in #37223 (comment)
If we do want to highlight that it's internally a Button, that should probably be made clearer in the docs.
Related to #37223 (comment), actually there's an interesting point that caught my attention:
I believe that Button
, when passed an href
prop, automatically renders as a <a />
instead of a <button />
In the light of the above, should we disallow href
as a prop from NavigatorLink
? I could then go ahead and add an additional paragraph at the end of the Props
section, similarly to https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/card/card/README.md?plain=1#L67-L69
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that
Button
, when passed anhref
prop, automatically renders as a<a />
instead of a<button />
In the light of the above, should we disallow
href
as a prop fromNavigatorLink
? I could then go ahead and add an additional paragraph at the end of theProps
section, similarly to https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/card/card/README.md?plain=1#L67-L69
Ah, that's right. Let's do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 5c584a5
packages/components/src/navigator/navigator-back-link/README.md
Outdated
Show resolved
Hide resolved
packages/components/src/navigator/navigator-back-link/README.md
Outdated
Show resolved
Hide resolved
b284a15
to
4377794
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
// eslint-disable-next-line no-restricted-imports | ||
import type { Ref } from 'react'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more here 🧹
// eslint-disable-next-line no-restricted-imports | |
import type { Ref } from 'react'; | |
import type { ForwardedRef } from 'react'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woops! Done in 47f4b1f
it( 'should escape the value of the `path` prop', () => { | ||
render( <MyNavigation /> ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kudos for adding a test!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Once the main "testing env" is set up, it actually becomes quite a trivial task
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for the path value escaping?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the path
prop is used as the value for an HTML attribute (part of the focus restoration logic), the value of path
needs to be a valid value for HTML attributes (meaning it can't contain a series of characters like single/double quotes, spaces, etc).
Therefore, the value of path
is escaped to make sure that it can be safely applied as an HTML attribute — and this test checks that behaviour
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks :)
…gatorBackButton`
7f1b257
to
5c584a5
Compare
Addressed the last round of feedback and rebased on top of Unless there's any other feedback, I'll merge if all checks pass! |
88de38a
to
bfb596d
Compare
bfb596d
to
e7866cb
Compare
Navigator
: add NavigatorLink
and NavigatorBackLink
componentsNavigator
: add NavigatorButton
and NavigatorBackButton
components
Navigator
: add NavigatorButton
and NavigatorBackButton
componentsNavigatorButton
and NavigatorBackButton
components
Description
Following the conversation in #37063 and the exploratory work done in #37223, this PR is the first step towards adding support for advanced focus restoration in
Navigator
. The plan is to have the work split across 3 PRs:Navigator
: add basic location history #37416)Navigator
: add focus restoration #38149)NavigationButton
andNavigationBackButton
components (this PR)This PR contains the following changes:
NavigatorButton
andNavigatorBackButton
Navigator
familyNavigator
component (in the post editor's preferences modal, and in the Global Styles sidebar)Note: compared to before, the HTML attribute used for the focus restoration is
id
(as opposed to a customdata
attribute). This can be changed through theattributeName
prop on theNavigatorLink
component, in case.Testing Instructions
No visual or behavioral changes are expected — this refactor should just make it easier to work with the
Navigator
components:Screenshots
Types of changes
New feature (introduction of two new components)
Checklist:
*.native.js
files for terms that need renaming or removal).