Skip to content

Commit 99a7bdd

Browse files
theskumarfrjo
andauthored
Update Django messages to reuse toast component used by htmx (#4385)
This updates the full page load to use the same mechanism to display the messages from `django.contrib.messages` as used during htmx request This is achieved by generating a javascript custom event for toast component instead of rendering html content. Add/update icons and color for different message types and handle long words and urls The web accessibility is maintained as the toast component handles it when it's is initialized. This is a further enhancement to #4374 to keep things DRY and consistent. --------- Co-authored-by: Fredrik Jonsson <[email protected]>
1 parent 97a589d commit 99a7bdd

File tree

5 files changed

+42
-76
lines changed

5 files changed

+42
-76
lines changed

hypha/static_src/tailwind/main.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
)
2222
10;
2323
}
24+
25+
/* see https://github.com/tailwindlabs/tailwindcss/pull/12128 */
26+
.break-anywhere {
27+
overflow-wrap: anywhere;
28+
}
2429
}

hypha/templates/base.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@
7171
{% include "includes/hijack-bar.html" %}
7272
{% endif %}
7373

74-
{% if messages %}
75-
{% include 'includes/messages.html' %}
76-
{% endif %}
77-
7874
{% block header %}{% endblock header %}
7975

8076
{% block content_wrapper %}
@@ -166,6 +162,11 @@
166162
{% include "includes/_modal-placeholder.html" %}
167163
{% include "includes/_toast-placeholder.html" %}
168164

165+
{# Display messages from django.contrib.messages as toast #}
166+
{% for message in messages %}
167+
<template x-init="$nextTick(() => {$dispatch('notice', {type: '{{ message.tags }}', text: '{{ message }}'})})"></template>
168+
{% endfor %}
169+
169170
<!-- We still need jQuery for select2, daterangepicker and a few custom scripts. -->
170171
<!-- Please do not add new features that require jQuery! -->
171172
<script src="{% static 'js/vendor/jquery.min.js' %}"></script>

hypha/templates/includes/_toast-placeholder.html

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
{% comment %}
33
How this works:
44
- A htmx response responds with either "showMessage" or "messages" "HX-Trigger" event.
5-
- htmx traps the event and create another event for the toast layer.
5+
- htmx traps the event and creates another event for the toast layer.
66
- The toast layer listens to the event and adds the toast to the list.
7+
- Toasts can be paused on hover and dismissed manually
8+
- Supports different message types with corresponding styles and icons
79
{% endcomment %}
810

911
<!-- Toast layer -->
1012
<script>
1113
function toastHandler() {
1214
"use strict";
13-
const AUTO_HIDE_TIMEOUT = 2500;
15+
const AUTO_HIDE_TIMEOUT = 3000;
1416
return {
1517
toasts: [],
1618
visible: [],
@@ -56,36 +58,28 @@
5658
},
5759
};
5860
}
61+
62+
const createNoticeEvent = (detail) => new CustomEvent("notice", {
63+
detail,
64+
bubbles: true,
65+
cancelable: true,
66+
composed: true
67+
});
68+
5969
htmx.on("showMessage", (e) => {
60-
let message_detail = { type: "success", text: e.detail.value };
61-
dispatchEvent(
62-
new CustomEvent("notice", {
63-
detail: message_detail,
64-
bubbles: true,
65-
cancelable: true,
66-
composed: true,
67-
})
68-
);
70+
dispatchEvent(createNoticeEvent({ type: "success", text: e.detail.value }));
6971
});
72+
7073
htmx.on("messages", (e) => {
71-
e.detail.value.forEach((message) => {
72-
let message_detail = { type: message.tags, text: message.message };
73-
console.log(message_detail);
74-
dispatchEvent(
75-
new CustomEvent("notice", {
76-
detail: message_detail,
77-
bubbles: true,
78-
cancelable: true,
79-
composed: true,
80-
})
81-
);
74+
e.detail.value.forEach(message => {
75+
dispatchEvent(createNoticeEvent({ type: message.tags, text: message.message }));
8276
});
8377
});
8478
</script>
8579

8680
<div
8781
x-data="toastHandler()"
88-
class="flex fixed inset-0 flex-col justify-start items-end px-4 w-screen h-screen pointer-events-none"
82+
class="flex fixed inset-0 z-50 flex-col justify-start items-end px-8 mx-auto w-screen h-screen pointer-events-none max-w-[2200px]"
8983
@notice.window="add($event.detail)"
9084
>
9185
<template x-for="toast of toasts" :key="toast.id">
@@ -96,30 +90,39 @@
9690
x-show="visible.includes(toast)"
9791
x-transition:enter="transition ease-in duration-200"
9892
x-transition:enter-start="transform opacity-0 translate-y-2"
99-
x-transition:enter-end="transform opacity-100"
93+
x-transition:enter-end="transform opacity-100 translate-y-0"
10094
x-transition:leave="transition ease-out duration-500"
10195
x-transition:leave-start="transform translate-x-0 opacity-100"
10296
x-transition:leave-end="transform translate-x-full opacity-0"
10397
@click="remove(toast.id)"
10498
@mouseenter="pause(toast.id)"
10599
@mouseleave="resume(toast.id)"
106-
class="relative py-3 px-4 mt-4 w-full max-w-full text-sm font-bold text-white rounded-md shadow-xl cursor-pointer sm:w-auto sm:max-w-sm group"
100+
class="relative py-3 px-4 mt-4 w-full max-w-full text-sm font-semibold text-white rounded-md shadow-xl cursor-pointer sm:w-auto sm:max-w-sm break-anywhere hyphens-auto group sm:min-w-64"
107101
:class="{
108-
'bg-gray-500': toast.type === 'debug',
109-
'bg-green-500': toast.type === 'success',
102+
'bg-neutral-500': toast.type === 'debug',
103+
'bg-green-600': toast.type === 'success',
110104
'bg-blue-500': toast.type === 'info',
111105
'bg-orange-500': toast.type === 'warning',
112106
'bg-red-500': toast.type === 'error',
113107
}"
114108
style="pointer-events:all"
115109
>
116-
<div class="flex gap-2 items-center">
110+
<div class="flex gap-4 items-center">
117111
<span x-show="toast.type == 'info'" aria-hidden="true">
118-
{% heroicon_solid "information-circle" size=24 class="inline-block rounded-full" %}
112+
{% heroicon_solid "light-bulb" size=24 class="inline-block rounded-full" %}
119113
</span>
120114
<span x-show="toast.type == 'success'" aria-hidden="true">
121115
{% heroicon_solid "check-circle" size=24 class="inline-block rounded-full" %}
122116
</span>
117+
<span x-show="toast.type == 'warning'" aria-hidden="true">
118+
{% heroicon_solid "exclamation-circle" size=24 class="inline-block rounded-full" %}
119+
</span>
120+
<span x-show="toast.type == 'error'" aria-hidden="true">
121+
{% heroicon_solid "exclamation-triangle" size=24 class="inline-block rounded-full" %}
122+
</span>
123+
<span x-show="toast.type == 'debug'" aria-hidden="true">
124+
{% heroicon_solid "wrench" size=24 class="inline-block rounded-full" %}
125+
</span>
123126
<span x-text="toast.text"></span>
124127
</div>
125128
<button class="absolute -top-3 -left-3 invisible p-1 transition-all group-hover:visible">

hypha/templates/includes/message_item.html

Lines changed: 0 additions & 37 deletions
This file was deleted.

hypha/templates/includes/messages.html

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)