2023-09-01 15:02:48 +02:00
|
|
|
;;;; alert.lisp
|
|
|
|
;;;;
|
|
|
|
;;;; This file defines a package for generating Bootstrap alert components
|
|
|
|
;;;; using Common Lisp macros.
|
2023-07-11 16:17:51 +02:00
|
|
|
|
2024-04-21 11:33:19 +02:00
|
|
|
(defpackage dev.metalisp.sbt/alert
|
2023-07-11 16:17:51 +02:00
|
|
|
(:use :cl)
|
|
|
|
(:export
|
|
|
|
:btn
|
2023-07-13 12:57:05 +02:00
|
|
|
:alert
|
|
|
|
:alert-primary
|
|
|
|
:alert-secondary
|
|
|
|
:alert-success
|
|
|
|
:alert-danger
|
|
|
|
:alert-warning
|
|
|
|
:alert-info
|
|
|
|
:alert-light
|
|
|
|
:alert-dark
|
|
|
|
:alert-dismiss-primary
|
|
|
|
:alert-dismiss-secondary
|
|
|
|
:alert-dismiss-success
|
|
|
|
:alert-dismiss-danger
|
|
|
|
:alert-dismiss-warning
|
|
|
|
:alert-dismiss-info
|
|
|
|
:alert-dismiss-light
|
|
|
|
:alert-dismiss-dark))
|
2023-07-11 16:17:51 +02:00
|
|
|
|
2024-04-21 11:33:19 +02:00
|
|
|
(in-package :dev.metalisp.sbt/alert)
|
2023-07-11 16:17:51 +02:00
|
|
|
|
|
|
|
(defmacro btn ()
|
|
|
|
"This macro generates the close button used in a Bootstrap alert when it is set to be dismissible."
|
2023-07-01 11:28:25 +02:00
|
|
|
|
2023-07-09 09:03:27 +02:00
|
|
|
`(spinneret:with-html
|
|
|
|
(:button :type "button"
|
|
|
|
:class "btn-close"
|
|
|
|
:data-bs-dismiss "alert"
|
|
|
|
:aria-label "Close")))
|
|
|
|
|
2023-08-09 15:06:10 +02:00
|
|
|
(defmacro alert ((&key (color "primary") (dismissible nil)) &body body)
|
2023-07-11 16:17:51 +02:00
|
|
|
"This macro generates a Bootstrap alert component.
|
|
|
|
|
2023-08-09 15:06:10 +02:00
|
|
|
COLOR: Specifies the alert type. Can be 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', or 'dark'. Defaults to 'primary'.
|
|
|
|
|
2023-07-14 10:06:50 +02:00
|
|
|
DISMISSIBLE: Specifies whether the alert is dismissible. If true, the alert includes a close button.
|
2023-08-09 15:06:10 +02:00
|
|
|
|
2023-07-14 10:06:50 +02:00
|
|
|
BODY: Specifies the content of the alert.
|
2023-07-13 21:43:34 +02:00
|
|
|
|
2023-07-14 10:06:50 +02:00
|
|
|
Example usage:
|
|
|
|
To create a basic alert of type 'danger':
|
2023-08-09 15:06:10 +02:00
|
|
|
(alert (:role \"danger\") \"This is a dangerous alert. Be careful!\")
|
2023-07-11 16:17:51 +02:00
|
|
|
|
2023-07-14 10:06:50 +02:00
|
|
|
To create a dismissible alert of type 'success':
|
|
|
|
(alert (:type \"success\" :dismissible t) \"Congratulations! You've successfully created a dismissible alert.\")"
|
2023-07-01 11:28:25 +02:00
|
|
|
`(spinneret:with-html
|
2023-07-08 22:25:20 +02:00
|
|
|
(:div :role "alert"
|
2023-08-09 15:06:10 +02:00
|
|
|
:class ,(concatenate 'string (format nil "alert alert-~a" color)
|
2023-07-11 16:17:51 +02:00
|
|
|
(if (null dismissible) nil " alert-dismissible"))
|
|
|
|
,(if (null dismissible) nil `(btn))
|
2023-07-08 22:25:20 +02:00
|
|
|
,@body)))
|
2023-07-01 13:16:55 +02:00
|
|
|
|
2023-08-09 15:06:10 +02:00
|
|
|
(defmacro define-alert (color &optional (dismissible nil))
|
2023-07-13 12:57:05 +02:00
|
|
|
"This macro defines a new macro for creating a Bootstrap alert of a specific type.
|
2023-07-01 13:16:55 +02:00
|
|
|
|
2023-08-09 15:06:10 +02:00
|
|
|
COLOR: The color of the alert (like 'primary', 'secondary', 'success', etc.).
|
|
|
|
|
2023-07-14 10:06:50 +02:00
|
|
|
DISMISSIBLE: (optional) Whether the alert should be dismissible.
|
2023-07-01 13:16:55 +02:00
|
|
|
|
2023-07-14 10:06:50 +02:00
|
|
|
The newly defined macro, when called, will generate HTML for a Bootstrap
|
|
|
|
alert of the specified type and dismissibility."
|
2023-08-09 15:06:10 +02:00
|
|
|
(let* ((macro-name (intern (string-upcase (concatenate 'string "ALERT-" (if (null dismissible) "" "DISMISS-") color)))))
|
2023-07-13 12:57:05 +02:00
|
|
|
`(defmacro ,macro-name (&body body)
|
2023-08-09 15:06:10 +02:00
|
|
|
`(alert (:color ,,color :dismissible ,,dismissible) ,@body))))
|
2023-07-01 13:16:55 +02:00
|
|
|
|
2023-08-09 15:06:10 +02:00
|
|
|
(defmacro define-alerts (colors)
|
2023-07-13 12:57:05 +02:00
|
|
|
"This macro generates specific alert macros based on the provided names.
|
2023-07-01 13:16:55 +02:00
|
|
|
|
2023-08-09 15:06:10 +02:00
|
|
|
COLORS: A list of alert type names. For each name in this list, a macro will
|
2023-07-14 10:06:50 +02:00
|
|
|
be generated: a non-dismissible alert and a dismissible alert of the
|
|
|
|
specified type."
|
2023-07-13 12:57:05 +02:00
|
|
|
`(progn
|
2023-08-09 15:06:10 +02:00
|
|
|
,@(loop for color in colors
|
|
|
|
for color-name = (string-downcase (string color))
|
2023-07-13 12:57:05 +02:00
|
|
|
collect `(progn
|
2023-08-09 15:06:10 +02:00
|
|
|
(define-alert ,color-name)
|
|
|
|
(define-alert ,color-name t)))))
|
2023-07-01 13:16:55 +02:00
|
|
|
|
2023-07-13 12:57:05 +02:00
|
|
|
(define-alerts (primary secondary success danger warning info light dark))
|