Update make-grid-class

This commit is contained in:
Marcus Kammer 2025-02-03 17:24:20 +01:00
parent 39848853b8
commit 55253c35a7
Signed by: marcuskammer
GPG key ID: C374817BE285268F

View file

@ -564,9 +564,14 @@ Example 3:
(defun combine-classes (&rest class-specs) (defun combine-classes (&rest class-specs)
(string-trim " " (format nil "~{~a ~}" (remove nil class-specs)))) (string-trim " " (format nil "~{~a ~}" (remove nil class-specs))))
(defun validate-col-value-p (value)
"Validate column-related numeric values."
(when (and (integerp value)
(<= 0 value 12))
t))
(defun make-grid-class (&key (defun make-grid-class (&key
row row
cols
col col
xs xs
sm sm
@ -583,6 +588,8 @@ Example 3:
order) order)
"Return a css class as string according to Bootstrap Grid System. "Return a css class as string according to Bootstrap Grid System.
---
Bootstraps grid system can adapt across all six default breakpoints, and any Bootstraps grid system can adapt across all six default breakpoints, and any
breakpoints you customize. The six default grid tiers are as follows: breakpoints you customize. The six default grid tiers are as follows:
@ -593,56 +600,53 @@ breakpoints you customize. The six default grid tiers are as follows:
- Extra large (xl) 1200px - Extra large (xl) 1200px
- Extra extra large (xxl) 1400px - Extra extra large (xxl) 1400px
All number values has to be >= 0 or <= 12 ---
except for gutters (:g :gx :gy) values has to be >= 0 or <= 5
ALIGN-ITEMS: \"start\", \"center\", \"end\"
ALIGN-SELF: \"start\", \"center\", \"end\"
JUSTIFY-CONTENT: \"start\", \"center\", \"end\", \"around\", \"between\", \"evenly\"
Example: Example:
(make-grid-class :row t :cols 2 :sm 2) -> \"row row-cols-2 row-cols-sm-2\" (make-grid-class :row t) -> \"row\"
(make-grid-class :cols 2) -> \"row row-cols-2\" (make-grid-class :row 2) -> \"row row-cols-2\"
(make-grid-class :col 5) -> \"col-5\" (make-grid-class :row 2 :sm 2) -> \"row row-cols-2 row-cols-sm-2\"
(make-grid-class :col 5) -> \"col col-5\"
(make-grid-class :col t :xs 12 :sm 6 :md :auto) -> \"col col-xs-12 col-sm-6 col-md-auto\" (make-grid-class :col t :xs 12 :sm 6 :md :auto) -> \"col col-xs-12 col-sm-6 col-md-auto\"
(make-grid-class :col :auto :sm t) -> \"col-auto col-sm\" (make-grid-class :col \"auto\" :sm t) -> \"col col-auto col-sm\"
(make-grid-class :col :auto) -> \"col-auto\" (make-grid-class :col \"auto\") -> \"col col-auto\"
(make-grid-class :sm 2) -> \"col-sm-2\" (make-grid-class :sm 2) -> \"col-sm-2\"
(make-grid-class :row t :col t) -> error (make-grid-class :row t :col t) -> error
(make-grid-class :col -1) -> error
(make-grid-class :col 13) -> error
(make-grid-class :g 0) -> \"row g-0\"" (make-grid-class :g 0) -> \"row g-0\""
;; First validate input (cond ((and row col)
(when (and row col) (error "Cannot specify both :row and :col"))
(error "Cannot specify both :row and :col")) ((and row align-self)
(error "Cannot specify both :row and :align-self"))
((and col (or g gx gy))
(error "Cannot specify both :col and gutters (:g :gx :gy)"))
((and col justify-content)
(error "Cannot specify both :col and :justify-content"))
((and col align-items)
(error "Cannot specify both :col and :align-items"))
((not (or row col))
(error "Specify at least :row or :col")))
(let ((class-strings '())
(breakpoints
(loop :for (key value) :on `(:xs ,xs :sm ,sm :md ,md :lg ,lg :xl ,xl :xxl ,xxl)
:by #'cddr
:when value :nconc (list key value))))
(let ((classes '()))
;; Handle row cases
(when row (when row
(push "row" classes) (push "row" class-strings)
(when cols (push (format nil "~{row-cols-~(~A~)-~A~^ ~}" breakpoints) class-strings)
(push (format nil "row-cols-~a" cols) classes))) (when (or (stringp row) (validate-col-value-p row))
(push (format nil "row-cols-~A" row) class-strings)))
;; Handle column cases (when col
(when (or col xs sm md lg xl xxl) (push "col" class-strings)
;; Base col class (push (format nil "~{col-~(~A~)-~A~^ ~}" breakpoints) class-strings)
(push (if (numberp col) (when (or (stringp col) (validate-col-value-p col))
(format nil "col-~a" col) (push (format nil "col-~A" col) class-strings)))
(when col "col"))
classes))
;; Handle breakpoint-specific classes (format nil "~{~A~^ ~}" class-strings)))
(flet ((add-breakpoint (prefix value)
(when value
(push (format nil "col-~a-~a"
prefix
(if (eq value t) ""
(if (eq value :auto) "auto" value)))
classes))))
(add-breakpoint "xs" xs)
(add-breakpoint "sm" sm)
(add-breakpoint "md" md)
(add-breakpoint "lg" lg)
(add-breakpoint "xl" xl)
(add-breakpoint "xxl" xxl))
;; Combine all classes, removing nil values and reversing to maintain order
(string-trim " " (format nil "~{~a~^ ~}"
(remove nil (nreverse classes))))))