radix-vue / radix-vue

Vue port of Radix UI Primitives. An open-source UI component library for building high-quality, accessible design systems and web apps.
https://radix-vue.com
MIT License
2.7k stars 143 forks source link

[Feature]: Add provider functions as alternatives to provider components #823

Closed remonke closed 1 week ago

remonke commented 1 month ago

Describe the feature

I suggest using functions to provide contexts instead of components. While provider components are common in React, I believe Vue's distinctive features should be embraced rather than emulating React. This approach reduces nesting and enables usage beyond templates. Provider components will remain, serving as wrappers for these provider functions. The syntax could be as follows:

<script setup lang="ts">
const nuxtUseId = () => useId()

radixProvideConfig({
  useId: nuxtUseId,
})
radixProvideTooltip({
  ignoreNonKeyboardFocus: true
})
radixProvideToast()
</script>

<template>
  <WhateverChild />
</template>

Instead of:

<script setup lang="ts">
const nuxtUseId = () => useId();
</script>

<template>
  <RadixConfigProvider :use-id="nuxtUseId">
    <RadixTooltipProvider ignore-non-keyboard-focus>
      <RadixToastProvider>
        <WhateverChild />
      </RadixToastProvider>
    </RadixTooltipProvider>
  </RadixConfigProvider>
</template>

Additional information

zernonia commented 1 month ago

The provider components were designed so that it brings clarity to dev, and make sure all the child component received the appropriate provided context, and not merely emulating React.

Also, some provider has functionalities other than just "provide" the context. Do check out

https://github.com/radix-vue/radix-vue/blob/3d8dcb43fce20a5574416651e6c1c38c315e7f75/packages/radix-vue/src/Tooltip/TooltipProvider.vue#L1-L98

and

https://github.com/radix-vue/radix-vue/blob/3d8dcb43fce20a5574416651e6c1c38c315e7f75/packages/radix-vue/src/Toast/ToastProvider.vue#L1-L93

remonke commented 1 month ago

But I don't understand why this couldn't be simply extracted to a function. These components don't render anything.

zernonia commented 1 month ago

Could you share on what do you mean by function?

The Provider component is needed for components. If you are referring to ConfigProvider, that's different case.

zernonia commented 1 week ago

As we cant replace provider components with provider functions (as mentioned here), I'm going to close this out.