Skip to content

Commit

Permalink
feat(Notification): support html with title and description slots (
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac authored Jul 20, 2023
1 parent 9fc786e commit df455db
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
10 changes: 9 additions & 1 deletion docs/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
<UDocsSearch :files="files" :links="navigation" />
</ClientOnly>

<UNotifications />
<UNotifications>
<template #title="{ title }">
<span v-html="title" />

Check warning on line 43 in docs/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

'v-html' directive can lead to XSS attack
</template>

<template #description="{ description }">
<span v-html="description" />

Check warning on line 47 in docs/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

'v-html' directive can lead to XSS attack
</template>
</UNotifications>
</div>
</template>

Expand Down
7 changes: 7 additions & 0 deletions docs/components/content/examples/NotificationExampleHtml.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup>
const toast = useToast()
</script>

<template>
<UButton label="Show toast" @click="toast.add({ title: 'Notification <i>italic</i>', description: 'This is an <u>underlined</u> and <b>bold</b> notification.' })" />
</template>
44 changes: 44 additions & 0 deletions docs/content/6.overlays/6.notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,50 @@ excludedProps:
---
::

## Slots

### `title` / `description`

Use the `#title` and `#description` slots to customize the Notification.

This can be handy when you want to display HTML content. To achieve this, you can define those slots in the top-level `<UNotifications />` component in your `app.vue` and use the `v-html` directive.

```vue [app.vue]
<template>
<UNotifications>
<template #title="{ title }">
<span v-html="title" />
</template>
<template #description="{ description }">
<span v-html="description" />
</template>
</UNotifications>
</template>
```

This way, you can use HTML tags in the `title` and `description` props when using `useToast`.

::component-example
#default
:notification-example-html

#code
```vue
<script setup>
const toast = useToast()
</script>
<template>
<UButton label="Show toast" @click="toast.add({ title: 'Notification <i>italic</i>', description: 'This is an <u>underlined</u> and <b>bold</b> notification.' })" />
</template>
```
::

::callout{icon="i-heroicons-light-bulb"}
Slots defined in the `<UNotifications />` component are automatically passed down to the `Notification` children.
::

## Props

:component-props
Expand Down
8 changes: 6 additions & 2 deletions src/runtime/components/overlays/Notification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

<div class="w-0 flex-1">
<p :class="ui.title">
{{ title }}
<slot name="title" :title="title">
{{ title }}
</slot>
</p>
<p v-if="description" :class="ui.description">
{{ description }}
<slot name="description" :description="description">
{{ description }}
</slot>
</p>

<div v-if="description && actions.length" class="mt-3 flex items-center gap-2">
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/components/overlays/Notifications.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
:class="notification.click && 'cursor-pointer'"
@click="notification.click && notification.click(notification)"
@close="toast.remove(notification.id)"
/>
>
<template v-for="(_, name) in $slots" #[name]="slotData">
<slot :name="name" v-bind="slotData" />
</template>
</UNotification>
</div>
</div>
</div>
Expand Down

1 comment on commit df455db

@vercel
Copy link

@vercel vercel bot commented on df455db Jul 20, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

ui – ./

ui-git-dev-nuxtlabs.vercel.app
ui.nuxtlabs.com
ui-nuxtlabs.vercel.app

Please sign in to comment.