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

fix(createReusableTemplate): camelize props #3253

Merged
merged 4 commits into from Jul 30, 2023

Conversation

94726
Copy link
Contributor

@94726 94726 commented Jul 24, 2023

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.
⚠️ Slowing down new functions

Warning: Slowing down new functions

As the VueUse audience continues to grow, we have been inundated with an overwhelming number of feature requests and pull requests. As a result, maintaining the project has become increasingly challenging and has stretched our capacity to its limits. As such, in the near future, we may need to slow down our acceptance of new features and prioritize the stability and quality of existing functions. Please note that new features for VueUse may not be accepted at this time. If you have any new ideas, we suggest that you first incorporate them into your own codebase, iterate on them to suit your needs, and assess their generalizability. If you strongly believe that your ideas are beneficial to the community, you may submit a pull request along with your use cases, and we would be happy to review and discuss them. Thank you for your understanding.


Description

This PR allows to mix attribute cases, similar to native Vue behavior.

Currently, attributes passed to reusableTemplates are passed to v-slot, as is. This leads to issues when trying to mix cases on define and reuse.

People might end up writing the following, which doesn't work

<script setup lang="ts">
import { createReusableTemplate } from '@vueuse/core'

const [DefineFoo, ReuseFoo] = createReusableTemplate<{ someMsg: string }>()
</script>

<template>
  <DefineFoo v-slot="{someMsg}">{{ someMsg }}</DefineFoo>

  <ReuseFoo some-msg="bar" />
</template>

Additional context

Technically vue exports camelize which could be used here, but for some reason hyphenate is not.
So I decided to copy these functions from vue for now.


🤖 Generated by Copilot at 453731b

Added prop name flexibility and improved code quality for createReusableTemplate. This function creates a Vue component from a template string and a set of props.

🤖 Generated by Copilot at 453731b

  • Copy hyphenate and camelize functions from vue core package to convert attribute keys between cases (link)
  • Use keysToCamelKebabCase function to create attrs object with both camelCase and kebab-case keys for reusable template component props (link, link)
  • Format type parameters for DefineTemplateComponent, ReuseTemplateComponent, ReusableTemplatePair, and createReusableTemplate to fit in one line for readability and consistency (link, link, link)

@antfu
Copy link
Member

antfu commented Jul 26, 2023

Weird, I wonder why Vue does not handle them. I'd like to see if there is a way to solve this better without needing to port code from Vue

@94726
Copy link
Contributor Author

94726 commented Jul 26, 2023

I was wondering too. Seems like Vue transforms slot attribute cases only at compile-time, see

Based on that, slot-props are never accessible in hyphenated format. So I think we could only camelize the attributes.
camelize() itself can also be importet from vue-demi, to avoid porting any vue code. But it is still marked as private so I'm unsure about that.

antfu and others added 2 commits July 28, 2023 02:11
Co-authored-by: Kasper Seweryn <github@wvffle.net>
@antfu antfu changed the title fix(createReusableTemplate): transform attribute cases fix(createReusableTemplate): camelize props Jul 27, 2023
@wvffle
Copy link
Contributor

wvffle commented Jul 27, 2023

Technically vue exports camelize which could be used here, but for some reason hyphenate is not.

In my opinion, the methods from Vue should be used instead of porting them. If anything changes in Vue, VueUse wouldn't need to be updated unless it was a breaking change. Also, if somebody wanted to use camelize/hyphenate from VueUse, they'd probably have exactly the same code twice in their production build. One from Vue used internally and one from VueUse used manually.

Also cacheStringFunction caches the function outputs without ever cleaning up the cache. Using a Vue's builtin function would result in lower memory consumption as we wouldn't need to create new cache object for attrs that are likely already cached.

If hyphenate is not exported, maybe it's a good idea to create a PR to export it rather than re-creating it? Provided it is even needed.

@wvffle
Copy link
Contributor

wvffle commented Jul 27, 2023

Fixes #3261

@antfu
Copy link
Member

antfu commented Jul 28, 2023

In practice, it would be hard to push this to happen, as it would require alignment across all Vue versions (2.6, 2.7, 3+), while Vue has been cautious about adding new public APIs. If you want, you can try to push it in Vue core. But note that even if it gets shipped, it will only be available in the next version, meaning we still need to port to support the older version.

In that sense, I'd think porting is the fast solution that is approachable. Maybe we could remove the cache, as they are probably not very perf-sensitive in VueUse.

@wvffle
Copy link
Contributor

wvffle commented Jul 28, 2023

it would require alignment across all Vue versions (2.6, 2.7, 3+)

You're right, I've missed that in my reasoning. Vue 2 does not export camelize. I guess, porting it is the right thing to do then.

Copy link
Contributor

@wvffle wvffle left a comment

Choose a reason for hiding this comment

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

How about using Vue's camelize if available and if not, then port?

@@ -0,0 +1,18 @@
// copied from vue: https://github.com/vuejs/core/blob/3be4e3cbe34b394096210897c1be8deeb6d748d8/packages/shared/src/general.ts#L90-L112
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// copied from vue: https://github.com/vuejs/core/blob/3be4e3cbe34b394096210897c1be8deeb6d748d8/packages/shared/src/general.ts#L90-L112
import { camelize as camelize_ } from 'vue-demi'
// copied from vue: https://github.com/vuejs/core/blob/3be4e3cbe34b394096210897c1be8deeb6d748d8/packages/shared/src/general.ts#L90-L112

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't this result in an error if someone is on a Vue version that doesn't export it?

I don't think it's worth it.

Copy link
Contributor

Choose a reason for hiding this comment

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

vue-demi returned an undefined on vue 2 when I was testing it

Copy link
Contributor Author

@94726 94726 Jul 29, 2023

Choose a reason for hiding this comment

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

Unfortunately not. That would require vue-demi to include such exports but it only re-exports everything from Vue, meaning that you basically try to import something non-existent from Vue.

I created an example for you https://stackblitz.com/edit/vitejs-vite-eikher?file=src%2FApp.vue

)

const camelizeRE = /-(\w)/g
export const camelize = cacheStringFunction((str: string): string => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export const camelize = cacheStringFunction((str: string): string => {
export const camelize = camelize_ ?? cacheStringFunction((str: string): string => {

@antfu antfu merged commit d79e174 into vueuse:main Jul 30, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants