xiaoluoboding / vue-sonner

🔔 An opinionated toast component for Vue & Nuxt.
https://vue-sonner.robertshaw.id
MIT License
932 stars 45 forks source link

Set Toast width #82

Closed IVoyt closed 2 months ago

IVoyt commented 2 months ago

Hi. Thanks for the great plugin.

Can you add option or maybe it already exists but I can't find it. I want to set custom toast width. How can I do this?

fdsssawe commented 2 months ago

@IVoyt Hey. You can set custom width for the toast using toastOptions.

If you wont to keept it static for all toast, you can just create your by importing it from the plugin, modify it using toastOptions, and when using it, import toaster from this file, not from the plugin, so :

<script setup lang="ts">
import { Toaster } from 'vue-sonner'

</script>
<template>
  <Toaster
      :toastOptions="{
        style :  { width: '150px' },
      }"
  />
</template>

For tailwind

<script setup lang="ts">
import { Toaster } from 'vue-sonner'

</script>
<template>
  <Toaster
      :toastOptions="{
        unstyled: true,
        classes: {
          toast: 'w-[150px]',
        },
      }"
  />
</template>

Usage

import { Toaster } from '@/components/ui/toast' //route to toaster you created earlier

...
<Toaster/>
<Button @click="() => toast(Title)">Show Toast</Button>
...

Result

image

IVoyt commented 2 months ago

@IVoyt Hey. You can set custom width for the toast using toastOptions.

If you wont to keept it static for all toast, you can just create your by importing it from the plugin, modify it using toastOptions, and when using it, import toaster from this file, not from the plugin, so :

<script setup lang="ts">
import { Toaster } from 'vue-sonner'

</script>
<template>
  <Toaster
      :toastOptions="{
        style :  { width: '150px' },
      }"
  />
</template>

For tailwind

<script setup lang="ts">
import { Toaster } from 'vue-sonner'

</script>
<template>
  <Toaster
      :toastOptions="{
        unstyled: true,
        classes: {
          toast: 'w-[150px]',
        },
      }"
  />
</template>

Usage

import { Toaster } from '@/components/ui/toast' //route to toaster you created earlier

...
<Toaster/>
<Button @click="() => toast(Title)">Show Toast</Button>
...

Result

image

Thanks for your response.

I just tested and I see it is reactive. Perfect!

But I thought I can use this approach to set different widths for different toasts. I expected by changing width for every triggering toast it will not override width for all toasts but apply different width for different toasts. Is there a way to implement my approach?

fdsssawe commented 2 months ago

@IVoyt Hey. You can set custom width for the toast using toastOptions. If you wont to keept it static for all toast, you can just create your by importing it from the plugin, modify it using toastOptions, and when using it, import toaster from this file, not from the plugin, so :

<script setup lang="ts">
import { Toaster } from 'vue-sonner'

</script>
<template>
  <Toaster
      :toastOptions="{
        style :  { width: '150px' },
      }"
  />
</template>

For tailwind

<script setup lang="ts">
import { Toaster } from 'vue-sonner'

</script>
<template>
  <Toaster
      :toastOptions="{
        unstyled: true,
        classes: {
          toast: 'w-[150px]',
        },
      }"
  />
</template>

Usage

import { Toaster } from '@/components/ui/toast' //route to toaster you created earlier

...
<Toaster/>
<Button @click="() => toast(Title)">Show Toast</Button>
...

Result image

Thanks for your response.

I just tested and I see it is reactive. Perfect!

But I thought I can use this approach to set different widths for different toasts. I expected by changing width for every triggering toast it will not override width for all toasts but apply different width for different toasts. Is there a way to implement my approach?

@IVoyt Yep. On function call :

toast('Event has been created', {
  style: {
    width : '150px',
  },
})

using tailwind

toast('Hello World', {
  unstyled: true,
  classes: {
    toast: 'w-[150px]',
  }
})
IVoyt commented 2 months ago

I don't have tailwind and yet I need to figure out how to integrate within my project :)

Anyways many thanks!

fdsssawe commented 2 months ago

@IVoyt

toast('Event has been created', {
  style: {
    width : '150px',
  },
})

this verison do not require tailwind.

IVoyt commented 2 months ago

@IVoyt

toast('Event has been created', {
  style: {
    width : '150px',
  },
})

this verison do not require tailwind.

That worked, thanks a lot!

fdsssawe commented 2 months ago

@IVoyt

toast('Event has been created', {
  style: {
    width : '150px',
  },
})

this verison do not require tailwind.

That worked, thanks a lot!

Радий що зміг допомогти )