ui icon indicating copy to clipboard operation
ui copied to clipboard

Multicolor theme custom switch

Open Cadeniuc opened this issue 1 year ago • 8 comments
trafficstars

Description

Hi guys, How can I add multicolor theme like on nuxt ui website ?

image

Cadeniuc avatar Apr 04 '24 19:04 Cadeniuc

You can find the relevant components in the documentation directory, under components/color-picker.

noook avatar Apr 04 '24 20:04 noook

You can find the relevant components in the documentation directory, under components/color-picker.

@noook Thanks for fast answer! I saw this, but how can I change the global variables on click ? Because I'm getting color value(hex), but don't understand how apply it globally😕

Cadeniuc avatar Apr 04 '24 20:04 Cadeniuc

Nuxt UI relies on the config in your app.config to determine your primary and your gray color. The ColorPicker component mutates the app.config so that all the Nuxt UI's components can read this new value, and use this color to style your components accordingly.

noook avatar Apr 04 '24 20:04 noook

This is for nuxt ui components. But what about custom components ?

Cadeniuc avatar Apr 04 '24 21:04 Cadeniuc

Nuxt UI exposes the useUI composable. This composable reads your app.config, and will perform a merge, or an override depending on the strategy you chose. This is for the underlying mechanism.

I think you could eventually augment the app config, and provide your own components config in it to customize them.

Now, for the actual implementation: a simple component is the UBadge. I'll let you have a look at the implementation, but the most relevant piece of code is the setup function. It reads your app.config given your component's key, then it computes the classes to give to your badge.

Now, the final magic: colors.ts plugin:

It defines CSS variables for primary and gray. Your components, when having the "primary" color set will be styled given the currently set CSS variables.

This is what I understood by navigating in the basecode in approximatively 15 minutes, what I said might not be 100% accurate, but you can inspire yourself from the codebase.

noook avatar Apr 04 '24 21:04 noook

@Cadeniuc are you sorted?

moshetanzer avatar Apr 11 '24 22:04 moshetanzer

@noook how can i persist the colors? When the page refreshes it set the primary and gray color as defined on app.config.ts

hamonCordova avatar Apr 12 '24 16:04 hamonCordova

You can check how the ColorPicker applies the new value. Once it is set in your storage (localStorage, cookie, or sessionStorage) you just need to make sure this data is read on page start.

https://github.com/nuxt/ui/blob/c75c0152cea7dab8b8888bb0651b72cd85577b19/docs/components/color-picker/ColorPicker.vue#L34-L43

On the documentation it is done here: https://github.com/nuxt/ui/blob/c75c0152cea7dab8b8888bb0651b72cd85577b19/docs/plugins/ui.ts#L31

noook avatar Apr 12 '24 16:04 noook