hsx/src/element.lisp

52 lines
1.3 KiB
Common Lisp
Raw Normal View History

2024-05-25 12:48:35 +00:00
(defpackage #:hsx/element
2024-05-25 03:00:39 +00:00
(:use #:cl)
2024-05-25 16:29:58 +00:00
(:export #:element-type
2024-05-25 03:00:39 +00:00
#:element-props
2024-05-25 15:57:06 +00:00
#:element-children
2024-05-25 03:00:39 +00:00
#:create-element
#:expand))
2024-05-25 12:48:35 +00:00
(in-package #:hsx/element)
2024-05-25 03:00:39 +00:00
(defclass element ()
2024-05-25 16:29:58 +00:00
((type
:reader element-type
:initarg :type)
2024-05-25 03:00:39 +00:00
(props
:reader element-props
2024-05-25 15:57:06 +00:00
:initarg :props)
(children
:reader element-children
:initarg :children)))
2024-05-25 03:00:39 +00:00
2024-05-25 16:29:58 +00:00
(defun create-element (type props &rest children)
2024-05-26 03:26:09 +00:00
(let ((elm (make-instance 'element
:type type
:props props
:children (flatten children))))
(prog1 elm
;dry-run to validate props
(expand elm))))
2024-05-25 03:00:39 +00:00
(defmethod expand ((elm element))
2024-05-25 16:29:58 +00:00
(with-accessors ((type element-type)
2024-05-25 15:57:06 +00:00
(props element-props)
(children element-children)) elm
2024-05-25 16:29:58 +00:00
(if (functionp type)
2024-05-26 04:57:51 +00:00
(apply type
(merge-children-into-props props children))
elm)))
2024-05-25 03:00:39 +00:00
(defun flatten (x)
(labels ((rec (x acc)
(cond ((null x) acc)
((atom x) (cons x acc))
(t (rec
(car x)
(rec (cdr x) acc))))))
(rec x nil)))
2024-05-26 04:57:51 +00:00
(defun merge-children-into-props (props children)
(append props
(and children
(list :children children))))