Skip to content
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

feat(kit,nuxt): add component priority to allow overriding #19252

Merged
merged 7 commits into from Mar 6, 2023

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Feb 23, 2023

πŸ”— Linked issue

resolves #19221

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

In line with the priority option support by auto-imported composables/utils, this PR adds a priority to components to allow them to override other components. By default, user components have a higher priority than components injected by modules without an explicit priority, but not higher than any Nuxt built-in components.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link

codesandbox bot commented Feb 23, 2023

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@danielroe danielroe added this to the v3.3 milestone Feb 24, 2023
@pi0
Copy link
Member

pi0 commented Feb 24, 2023

I have initially removed priority (from Nuxt 2 => 3) in favor of respecting the order simply from the components: [] array (consistent with layers). Supporting kit utils seems a good idea πŸ‘πŸΌ because it cannot control the array push order. (But maybe user components should have something higher than modules by default?)

Regarding built-ins, I agree with your comment in #19221 (comment). Same for built-in composables, fetch, etc, With auto imports and implicit sources, allowing a built-in to be overridden by the user, can cause unwanted behavior by any module, theme layer, and code that was depending on Nuxt behavior and now is changed! (a layer can break another layer too). So i believe we might give Nuxt Built-ins priority too and document only as advanced escape hatch to override them if really needed.

Default priority: Built-ins > User Components > Layer 1 > Layer 2 > Module Aded [can be overridden with priority ]

Copy link
Member

Atinux commented Feb 25, 2023

What is the main advantage of overwriting built-ins components?

It can also create confusion for team members working on a Nuxt project with overwritten built-in components.

Co-authored-by: Damian GΕ‚owala <48835293+DamianGlowala@users.noreply.github.com>
@danielroe
Copy link
Member Author

The Nuxt component built-ins are:

  • NuxtWelcome
  • NuxtErrorBoundary
  • ClientOnly
  • DevOnly
  • ServerPlaceholder
  • NuxtLink
  • NuxtLoadingIndicator
  • NuxtIsland
  • NuxtLayout
  • NuxtPage
  • NoScript, Link, Base, Title, Meta, Style, Head, Html and Body

Of those, I think that it would be safe (and good DX) if users could provide their own implementations for <NuxtLink> and <NuxtLoadingIndicator>. Part of the point would be that by overriding their custom behaviour would then be picked up by any modules or layers (for example) that did use those components.

For the other built-in components, their priority when registering them that ensures that it would require extra effort to override them, but still allows an escape hatch for advanced use cases.

wdyt?

maybe user components should have something higher than modules by default

That is the implementation here - user components are given a default priority of 1, which means they will override module components.

@pi0
Copy link
Member

pi0 commented Feb 27, 2023

Nice summary and LGTM.

Btw this also opens a design in consistency in Nuxt where some built-ins are exceptions to be globally overriding. they both support custom slots for being customized. For the component libraries needing to extend, they could support opt-in NuxtCustomLink by registration detection (we used similar strategy for BV links integrating with Nuxt). Do you think there are common enough usecases that need this kind of overriding often in userland project?

@danielroe danielroe merged commit 129bb4f into main Mar 6, 2023
@danielroe danielroe deleted the feat/component-priority branch March 6, 2023 11:33
@danielroe danielroe mentioned this pull request Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't create a <Link> component.
5 participants