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(module)!: use tailwind-merge for app.config & move config to components & type props #692

Merged
merged 23 commits into from
Sep 20, 2023

Conversation

benjamincanac
Copy link
Member

@benjamincanac benjamincanac commented Sep 16, 2023

Resolves #703
Resolves #628
Resolves #669
Resolves #442

  1. This PR will reduce the overall bundle size by only loading the config of the components you're actually using.

  2. Props having a validator are now typed to have autocomplete:

CleanShot 2023-09-20 at 17 56 39@2x
  1. app.config.ts is now smartly merged with the default config using tailwind-merge (like the ui prop`)

This will allow you (like explained in #509) to configure the components without rewriting everything:

export default defineAppConfig({
  ui: {
    primary: 'green',
    gray: 'slate',
    button: {
      color: {
        white: {
          solid: 'shadow-none'
        }
      }
    }
  }
})

The button.color.white.solid is 'shadow-sm ring-1 ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white hover:bg-gray-50 disabled:bg-white dark:bg-gray-900 dark:hover:bg-gray-800/50 dark:disabled:bg-gray-900 focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400', doing so will only remove the shadow-sm.

The merging strategy defaults to merge but can be changed to its previous behaviour override to avoid breaking changes.

export default defineAppConfig({
  ui: {
    strategy: 'override',
    primary: 'green'
  }
})

It is now also possible to configure the strategy per component instance using the ui prop:

<UButton
  to="https://github.com/nuxt/ui"
  :ui="{
    strategy: 'override',
    color: {
      white: {
        solid: 'bg-white dark:bg-gray-900'
      }
    }
  }"
/>

This will override the solid variant of the button completely instead of merging the classes.

@vercel
Copy link

vercel bot commented Sep 16, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
ui ✅ Ready (Inspect) Visit Preview Sep 20, 2023 3:59pm

@benjamincanac benjamincanac changed the title feat: use tailwind-merge for app.config by moving to components feat: use tailwind-merge for app.config & move config to components Sep 16, 2023
@benjamincanac benjamincanac changed the title feat: use tailwind-merge for app.config & move config to components feat(module)!: use tailwind-merge for app.config & move config to components Sep 18, 2023
Copy link
Member Author

@romhml @aditio-eka To let you know, I just merged dev into this PR with #638 and I had to change a few things already due to my refactor:

Long story short, instead of omitting id from attrs, I override the id prop by the one from useFormGroup composable. Let me know if you see any issue with this!

@benjamincanac benjamincanac changed the title feat(module)!: use tailwind-merge for app.config & move config to components feat(module)!: use tailwind-merge for app.config & move config to components & type props Sep 20, 2023
@benjamincanac benjamincanac merged commit 34d2f57 into dev Sep 20, 2023
1 check passed
@benjamincanac benjamincanac deleted the feat/628 branch September 20, 2023 16:07
Copy link
Contributor

Choose a reason for hiding this comment

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

Hello @benjamincanac, I found a bug caused by this function. You can see the details here: #742. It's missing reactivity from the ui prop and the class attribute.

I suggest changing this source code to

type UIProps<T> = {
  ui: Partial<T & { strategy: Strategy }>
}

export const useUI = <T>(key, $props: UIProps<T>, $config?: T, { mergeWrapper = false }: { mergeWrapper?: boolean } = {}) => {
  const $attrs = useAttrs()
  const appConfig = useAppConfig()

  const ui = computed(() => {
    return mergeConfig<T>(
      $props.ui?.strategy || (appConfig.ui?.strategy as Strategy),
      mergeWrapper ? { wrapper: $attrs?.class } : {},
      $props.ui || {},
      process.dev ? get(appConfig.ui, key, {}) : {},
      toValue($config || {})
    )
  })
  const attrs = computed(() => omit($attrs, ['class']))
  const attrsClass = computed(() => mergeWrapper ? undefined : $attrs?.class as string)

  return {
    ui,
    attrs,
    attrsClass
  }
}

So, useUI should be used like this

const { ui, attrs, attrsClass } = useUI('button', props, config)

Copy link
Member Author

Choose a reason for hiding this comment

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

This does makes sense, do you plan to open a PR to fix this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I have created a PR. You can view it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants