-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path01-hello-world.el
More file actions
146 lines (115 loc) · 3.72 KB
/
01-hello-world.el
File metadata and controls
146 lines (115 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;; 01-hello-world.el --- Basic vui.el examples -*- lexical-binding: t -*-
;; This file contains basic examples from the Getting Started guide.
;; Evaluate the entire buffer with M-x eval-buffer, then run individual demos.
;;; Code:
(require 'vui)
;;; Example 1: Hello World
;; The simplest possible component
(vui-defcomponent hello-world ()
:render (vui-text "Hello, World!"))
(defun vui-example-hello-world ()
"Run the Hello World example."
(interactive)
(vui-mount (vui-component 'hello-world) "*vui-hello-world*"))
;;; Example 2: Click Counter
;; Basic state and interactivity
(vui-defcomponent click-counter ()
:state ((count 0))
:render
(vui-fragment
(vui-text (format "Clicked: %d times" count))
(vui-newline)
(vui-button "Click me!"
:on-click (lambda ()
(vui-set-state :count (1+ count))))))
(defun vui-example-counter ()
"Run the Click Counter example."
(interactive)
(vui-mount (vui-component 'click-counter) "*vui-counter*"))
;;; Example 3: Greeter with Props
;; Passing data to components
(vui-defcomponent greeter (name)
:render (vui-text (format "Hello, %s!" name)))
(defun vui-example-greeter ()
"Run the Greeter example."
(interactive)
(vui-mount (vui-component 'greeter :name "Alice") "*vui-greeter*"))
;;; Example 4: Collapsible Card
;; Conditional rendering and composition
(vui-defcomponent greeting-card (title)
:state ((expanded nil))
:render
(vui-fragment
(vui-button (if expanded "▼" "▶")
:on-click (lambda ()
(vui-set-state :expanded (not expanded))))
(vui-text (format " %s" title))
(vui-newline)
(when expanded
(vui-fragment
(vui-text " Welcome to vui.el!")
(vui-newline)
(vui-text " This is a collapsible card.")))))
(defun vui-example-card ()
"Run the Collapsible Card example."
(interactive)
(vui-mount (vui-component 'greeting-card :title "My Card") "*vui-card*"))
;;; Example 5: Name Form
;; Text input and reactive updates
(vui-defcomponent name-form ()
:state ((name ""))
:render
(vui-fragment
(vui-text "Enter your name: ")
(vui-field
:value name
:size 20
:on-change (lambda (new-value)
(vui-set-state :name new-value)))
(vui-newline)
(vui-newline)
(vui-text (if (string-empty-p name)
"Type something above..."
(format "Hello, %s!" name)))))
(defun vui-example-name-form ()
"Run the Name Form example."
(interactive)
(vui-mount (vui-component 'name-form) "*vui-name-form*"))
;;; Example 6: Todo Item (Exercise Solution)
;; Solution to the exercise from Getting Started
(vui-defcomponent todo-item-example (text)
:state ((done nil))
:render
(vui-hstack
(vui-button (if done "X" " ")
:on-click (lambda ()
(vui-set-state :done (not done))))
(vui-text text :face (when done 'shadow))))
(vui-defcomponent todo-items-demo ()
:render
(vui-fragment
(vui-text "Todo Items (click to toggle):")
(vui-newline)
(vui-newline)
(vui-component 'todo-item-example :text "Learn vui.el basics")
(vui-newline)
(vui-component 'todo-item-example :text "Build a simple app")
(vui-newline)
(vui-component 'todo-item-example :text "Read the documentation")))
(defun vui-example-todo-item ()
"Run the Todo Item example (exercise solution)."
(interactive)
(vui-mount (vui-component 'todo-items-demo) "*vui-todo-items*"))
;;; Run All Examples
(defun vui-example-all ()
"Run all hello world examples."
(interactive)
(vui-example-hello-world)
(vui-example-counter)
(vui-example-greeter)
(vui-example-card)
(vui-example-name-form)
(vui-example-todo-item)
(message "All examples running. Check *vui-* buffers."))
(provide '01-hello-world)
;;; 01-hello-world.el ends here