Re-design the accordion macro

This commit is contained in:
Marcus Kammer 2024-02-03 18:48:00 +01:00
parent b47bae72f3
commit c8928bff1a
2 changed files with 31 additions and 21 deletions

View file

@ -74,7 +74,7 @@ Example:
(:div :class "accordion-item"
,@body)))
(defmacro accordion (id &rest rest)
(defmacro accordion-old (id &rest rest)
"This macro generates an accordion-style collapsible list with Bootstrap.
ID: Specifies a unique identifier for the accordion. Defaults to 'accordionExample'.
@ -97,3 +97,13 @@ Example:
collect (destructuring-bind (&key target name show content) item
`(item (header ,target ,name ,show)
(collapse ,id ,target ,show ,content)))))))
(defmacro accordion ((&key id flush) &body body)
(let ((class (concatenate 'string "accordion" (when flush " accordion-flush"))))
`(spinneret:with-html
(:div :class ,class
:id ,id
,@(loop for (title . content) in body
collect `(:div :class "accordion-item"
(:h5 :class "accordion-header" ,title)
(:div :class "accordion-body" ,content)))))))

View file

@ -42,28 +42,28 @@
(ok (search "aria-controls=#collapseOne" result))
(ok (search "Some content" result)))))
(deftest test-accordion
(let ((result (spinneret:with-html-string
(accordion "accordionExample"
(:target "collapseOne" :name "Accordion Item #1" :show t :content "This is the first item's accordion body.")
(:target "collapseTwo" :name "Accordion Item #2" :content "This is the second item's accordion body.")
(:target "collapseThree" :name "Accordion Item #3" :content "This is the third item's accordion body.")))))
(testing "Generates correct HTML for accordion"
(ok (search "class=accordion" result))
(ok (search "id=accordionExample" result))
(ok (search "class=accordion-item" result))
(ok (search "class=accordion-header" result))
(ok (search "class=accordion-button" result))
(ok (search "class=\"accordion-collapse collapse show\"" result))
(ok (search "data-bs-target=#collapseOne" result))
(ok (search "aria-expanded=true" result))
(ok (search "aria-controls=#collapseOne" result))
(ok (search "This is the first item's accordion body." result))
(ok (search "This is the second item's accordion body." result))
(ok (search "This is the third item's accordion body." result)))))
;; (deftest test-accordion
;; (let ((result (spinneret:with-html-string
;; (accordion "accordionExample"
;; (:target "collapseOne" :name "Accordion Item #1" :show t :content "This is the first item's accordion body.")
;; (:target "collapseTwo" :name "Accordion Item #2" :content "This is the second item's accordion body.")
;; (:target "collapseThree" :name "Accordion Item #3" :content "This is the third item's accordion body.")))))
;; (testing "Generates correct HTML for accordion"
;; (ok (search "class=accordion" result))
;; (ok (search "id=accordionExample" result))
;; (ok (search "class=accordion-item" result))
;; (ok (search "class=accordion-header" result))
;; (ok (search "class=accordion-button" result))
;; (ok (search "class=\"accordion-collapse collapse show\"" result))
;; (ok (search "data-bs-target=#collapseOne" result))
;; (ok (search "aria-expanded=true" result))
;; (ok (search "aria-controls=#collapseOne" result))
;; (ok (search "This is the first item's accordion body." result))
;; (ok (search "This is the second item's accordion body." result))
;; (ok (search "This is the third item's accordion body." result)))))
(deftest test-accordion-new-approach
(let ((result (with-output-to-string (spinneret:*html*) (accordion (:id "accordionExample" :flush t) (("Accordion Item #1" . "This is the first item's accordion body. It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.") ("Accordion Item #2" . "This is the second item's accordion body. It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.") ("Accordion Item #3" . "This is the third item's accordion body. It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow."))))))
(let ((result (with-output-to-string (spinneret:*html*) (accordion (:id "accordionExample" :flush t) ("Accordion Item #1" . "This is the first item's accordion body. It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.") ("Accordion Item #2" . "This is the second item's accordion body. It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.") ("Accordion Item #3" . "This is the third item's accordion body. It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.")))))
(testing "Generates correct HTML for accordion"
(ok (search "class=accordion" result))
(ok (search "id=accordionExample" result))