summaryrefslogtreecommitdiff
path: root/examples/calculator/style.css
blob: 605c7edaa6a71205aeef4e4a6f49139cb116b141 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
:root {
  --cssx-children: main#container;
}

html, body {
  margin: 0;
  padding: 0;
  background-color: #e2e8f0;
  font-size: 16px;
}
body * {
  box-sizing: border-box;
  font-family: Courier, monospace;
}

#container {
  --num1: '';
  --num2: '';
  --operation: '';

  max-width: 400px;
  margin: 2rem auto;
  padding: 1rem;
  background-color: #020617;
  color: #e2e8f0;

  --cssx-children: #display hr#sep0 #buttons;
}

#display {
  padding: 0 1rem;
  line-height: 1.7em;
  font-size: 2rem;
  height: 3rem;
  background-color: #0f172a;
  text-align: right;
}
#display::after {
  content: var(--num1) var(--operation) var(--num2);
}

#buttons {
  --cssx-children:
    h(div#toprow.horizontal, map(), seq(
      button#btn-clear,
      button#btn-run,
    ))
    div#buttons-numbers
    div#buttons-operators.horizontal
  ;
}
#buttons > * {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

#buttons-operators {
  --cssx-children:
    instance(button#btn-op, map(--op: "+"))
    instance(button#btn-op, map(--op: "-"))
    instance(button#btn-op, map(--op: "*"))
    instance(button#btn-op, map(--op: "/"))
    instance(button#btn-op, map(--op: "!"))
  ;
}

#buttons-numbers {
  --cssx-children:
    instance(button#btn-num, map(--n: "9"))
    instance(button#btn-num, map(--n: "8"))
    instance(button#btn-num, map(--n: "7"))
    instance(button#btn-num, map(--n: "6"))
    instance(button#btn-num, map(--n: "5"))
    instance(button#btn-num, map(--n: "4"))
    instance(button#btn-num, map(--n: "3"))
    instance(button#btn-num, map(--n: "2"))
    instance(button#btn-num, map(--n: "1"))
    instance(button#btn-num, map(--n: "0"))
  ;
}
#buttons-numbers > * {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
}

[data-instance=btn-num]::after { content: var(--n); }
[data-instance=btn-num] {
  width: 100%;
  --cssx-on-click:
    if(get-var(--operation),
      if(equals(get-var(--operation), '!'),
        '',
        update('container', --num2, string(get-var(--num2), get-var(--n)))),
      update('container', --num1, string(get-var(--num1), get-var(--n))),
    );
}

[data-instance=btn-op]::after { content: var(--op); }
[data-instance=btn-op] {
  width: 100%;
  --cssx-on-click: update('container', --operation, get-var(--op));
}

#btn-run::after { content: '='; }
#btn-run {
  --factorial: func(--n: number)
    if(lte(get-var(--n), 1), 1,
      calc(
        get-var(--n)
        * call(--factorial, map(--n: calc(get-var(--n) - 1)))
      ));

  --cssx-on-click:
    update('container', --num1,
      if(equals(get-var(--operation), '!'),
        call(--factorial, map(--n: get-var(--num1))),
        if(get-var(--num2),
          js-eval(string(get-var(--num1), get-var(--operation), get-var(--num2))),
          get-var(--num1)
        )
      )
    )
    update('container', --operation, '')
    update('container', --num2, '');
}

#btn-clear::after { content: 'clear'; }
#btn-clear {
  --cssx-on-click:
    update('container', --num1, '')
    update('container', --operation, '')
    update('container', --num2, '');
}

button {
  width: 100%;
  padding: 0.5rem 0;
  border: none;
  background-color: #475569;
  color: white;
  font-weight: bold;
  font-size: 1.3rem;
  cursor: pointer;
}
button:hover { background-color: #334155; }
button:active { background-color: #64748b; }

body .horizontal > * {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 2px;
}