dev.metalisp.sbt/docs/component/accordion.org
2023-10-02 11:24:12 +02:00

4.9 KiB

Using cl-sbt/accordion Macros in a Web Application

Introduction

The accordion component is a popular UI element that helps to organize and present content in a space-efficient manner. Accordions consist of a list of headers that users can click on to toggle the visibility of associated content. This is particularly useful for presenting large amounts of information in a confined space while keeping the interface clean and uncluttered. The Accordion component is perfect for FAQ sections, multi-part forms, or any situation where you need to present the user with a range of options or information, but you want to keep the interface uncluttered by hiding content that isn't immediately necessary.

Bootstrap Accordions

Bootstrap is a popular front-end framework that provides ready-to-use components for building responsive and modern web applications. One of its components is the accordion, which enhances the user experience by providing a user-friendly way to display and interact with content.

How Bootstrap Accordions Work

Bootstrap accordions are created using HTML, CSS, and JavaScript. The Bootstrap JavaScript library adds the necessary interactivity to the accordion. Here's how it works:

  • Each accordion item consists of a header and content section.
  • Clicking on an accordion item's header toggles the visibility of its content.
  • By default, only one item's content is visible at a time. Clicking on a header closes any open content before opening the clicked item's content.
  • Bootstrap's JavaScript library manages the animations and transitions for smooth content expansion and collapse.

Integrating cl-sbt/accordion Macros

To create Bootstrap-style accordions in your Common Lisp web application, you can use the cl-sbt/accordion macros. These macros provide a convenient way to generate the necessary HTML structure for accordion components. Here's how you can integrate them into your project:

  (defpackage my-web-app
    (:use :cl :cl-sbt/accordion)
    (:export :generate-accordion-page))

  (in-package :my-web-app)

  (defun generate-accordion-page ()
    "Generates an HTML page with an accordion using cl-sbt/accordion macros."
    (spinneret:with-html-string
      (:html
       (:head
        (:title "Accordion Example")
        ;; Include Bootstrap CSS and JavaScript links here
        )
       (:body
         (:h1 "Accordion Example")
         (accordion "myAccordion"
                    (:target "collapseOne" :name "Section 1" :show t :content "Content for section 1.")
                    (:target "collapseTwo" :name "Section 2" :content "Content for section 2.")
                    (:target "collapseThree" :name "Section 3" :content "Content for section 3."))
         ;; Include Bootstrap JavaScript initialization script here
         ))))

  (generate-accordion-page)
  <html lang=en>
   <head>
    <meta charset=UTF-8>
    <title>Accordion Example</title>
   </head>
   <body>
    <h1>Accordion Example</h1>
    <div class=accordion id=myAccordion>
     <div class=accordion-item>
      <h2 class=accordion-header>
       <button class=accordion-button type=button data-bs-toggle=collapse
               data-bs-target=#collapseOne aria-expanded=true
               aria-controls=#collapseOne>Section 1</button>
      </h2>
      <div class="accordion-collapse collapse show" id=collapseOne
           data-bs-parent=#myAccordion>
       <div class=accordion-body>
        Content for section 1.
       </div>
      </div>
     </div>
     <div class=accordion-item>
      <h2 class=accordion-header>
       <button class=accordion-button type=button data-bs-toggle=collapse
               data-bs-target=#collapseTwo aria-expanded=false
               aria-controls=#collapseTwo>Section 2</button>
      </h2>
      <div class="accordion-collapse collapse" id=collapseTwo
           data-bs-parent=#myAccordion>
       <div class=accordion-body>
        Content for section 2.
       </div>
      </div>
     </div>
     <div class=accordion-item>
      <h2 class=accordion-header>
       <button class=accordion-button type=button data-bs-toggle=collapse
               data-bs-target=#collapseThree aria-expanded=false
               aria-controls=#collapseThree>Section 3</button>
      </h2>
      <div class="accordion-collapse collapse" id=collapseThree
           data-bs-parent=#myAccordion>
       <div class=accordion-body>
        Content for section 3.
       </div>
      </div>
     </div>
    </div>
   </body>
  </html>

This example demonstrates how to integrate the cl-sbt/accordion macros into a web application to create an interactive accordion component. The macros help generate the necessary HTML structure for the accordion, which allows users to navigate through content conveniently.