@onivoro/server-html 24.25.1 → 24.25.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -244
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
# @onivoro/server-html
|
|
2
2
|
|
|
3
|
-
A TypeScript library for server-side HTML generation with a functional approach.
|
|
3
|
+
A TypeScript library for server-side HTML generation with a functional approach. Philosophically similar to JSX except there's no compilation.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- **Functional HTML Generation**: Create HTML elements using functional composition
|
|
8
8
|
- **Type-Safe**: Full TypeScript support with proper type definitions
|
|
9
|
-
- **Email Templates**: Pre-built email body and table components
|
|
10
|
-
- **Styling Support**: Built-in style constants and inline styling utilities
|
|
11
|
-
- **Flexible Attributes**: Support for CSS classes, inline styles, and custom attributes
|
|
12
|
-
- **Element Primitives**: Complete set of HTML element builders
|
|
13
9
|
- **Server-Side Focused**: Optimized for server-side rendering scenarios
|
|
14
10
|
|
|
15
11
|
## Installation
|
|
@@ -20,35 +16,10 @@ npm install @onivoro/server-html
|
|
|
20
16
|
|
|
21
17
|
## Quick Start
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
import { div, h1, p, button } from '@onivoro/server-html';
|
|
25
|
-
|
|
26
|
-
// Basic element creation
|
|
27
|
-
const title = h1(['Welcome to My Site']);
|
|
28
|
-
const description = p(['This is a sample paragraph.']);
|
|
29
|
-
|
|
30
|
-
// Element with attributes and styling
|
|
31
|
-
const styledDiv = div([title, description], {
|
|
32
|
-
cssClass: 'container',
|
|
33
|
-
style: {
|
|
34
|
-
'max-width': '800px',
|
|
35
|
-
margin: '0 auto',
|
|
36
|
-
padding: '2rem'
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// Button with built-in styling
|
|
41
|
-
const actionButton = button(['Click Me'], {
|
|
42
|
-
href: '/action',
|
|
43
|
-
style: { 'background-color': '#007bff', color: 'white' }
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
## DOM API Compatibility
|
|
47
|
-
|
|
48
|
-
Use the familiar $-prefixed element factories from @vanilla-mint/dom for server-side rendering:
|
|
19
|
+
Use the $-prefixed element factories from @vanilla-mint/dom server-side:
|
|
49
20
|
|
|
50
21
|
```typescript
|
|
51
|
-
import { $div, $h1, $p
|
|
22
|
+
import { $div, $h1, $p } from '@onivoro/server-html';
|
|
52
23
|
|
|
53
24
|
const html = $div({
|
|
54
25
|
className: 'container',
|
|
@@ -60,218 +31,6 @@ const html = $div({
|
|
|
60
31
|
});
|
|
61
32
|
```
|
|
62
33
|
|
|
63
|
-
## Core Concepts
|
|
64
|
-
|
|
65
|
-
## Core Concepts
|
|
66
|
-
|
|
67
|
-
### Element Builders
|
|
68
|
-
|
|
69
|
-
The library provides functional element builders for all common HTML elements:
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
import {
|
|
73
|
-
div, h1, h2, h3, h4, h5, h6, p,
|
|
74
|
-
button, anchor, img, table, thead,
|
|
75
|
-
tbody, tr, th, td, main, header
|
|
76
|
-
} from '@onivoro/server-html';
|
|
77
|
-
|
|
78
|
-
// All elements follow the same pattern:
|
|
79
|
-
// elementName(content, attributes?)
|
|
80
|
-
const heading = h1(['Page Title']);
|
|
81
|
-
const paragraph = p(['Some content'], { cssClass: 'lead' });
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Attributes and Styling
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
import { div } from '@onivoro/server-html';
|
|
88
|
-
|
|
89
|
-
const styledElement = div(['Content'], {
|
|
90
|
-
// CSS class
|
|
91
|
-
cssClass: 'my-class',
|
|
92
|
-
|
|
93
|
-
// Inline styles
|
|
94
|
-
style: {
|
|
95
|
-
'background-color': '#f8f9fa',
|
|
96
|
-
padding: '1rem',
|
|
97
|
-
'border-radius': '4px'
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
// Custom attributes
|
|
101
|
-
id: 'unique-id',
|
|
102
|
-
'data-testid': 'test-element'
|
|
103
|
-
});
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## Email Templates
|
|
107
|
-
|
|
108
|
-
### Email Body Component
|
|
109
|
-
|
|
110
|
-
Create structured email templates with headers and content:
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
import { emailBody } from '@onivoro/server-html';
|
|
114
|
-
|
|
115
|
-
const email = emailBody(
|
|
116
|
-
'Welcome!', // title
|
|
117
|
-
'Thanks for joining us', // subtitle
|
|
118
|
-
[ // content markup
|
|
119
|
-
'<p>We\'re excited to have you on board.</p>',
|
|
120
|
-
'<p>Get started by exploring our features.</p>'
|
|
121
|
-
],
|
|
122
|
-
'https://example.com/logo.png', // optional logo URL
|
|
123
|
-
{ // optional extra styles
|
|
124
|
-
'background-color': '#ffffff',
|
|
125
|
-
'font-family': 'Arial, sans-serif'
|
|
126
|
-
}
|
|
127
|
-
);
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### Data Tables
|
|
131
|
-
|
|
132
|
-
Generate HTML tables with automatic styling:
|
|
133
|
-
|
|
134
|
-
```typescript
|
|
135
|
-
import { table } from '@onivoro/server-html';
|
|
136
|
-
|
|
137
|
-
const dataTable = table(
|
|
138
|
-
['Name', 'Email', 'Role'], // columns
|
|
139
|
-
[ // rows
|
|
140
|
-
['John Doe', 'john@example.com', 'Admin'],
|
|
141
|
-
['Jane Smith', 'jane@example.com', 'User'],
|
|
142
|
-
['Bob Johnson', 'bob@example.com', 'Editor']
|
|
143
|
-
]
|
|
144
|
-
);
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
## Styling Utilities
|
|
148
|
-
|
|
149
|
-
### Pre-built Styles
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
import { buttonStyles, fontStyles } from '@onivoro/server-html';
|
|
153
|
-
|
|
154
|
-
// buttonStyles includes:
|
|
155
|
-
// - padding, border, border-radius
|
|
156
|
-
// - font-weight, text-align
|
|
157
|
-
// - display and box-sizing
|
|
158
|
-
|
|
159
|
-
// fontStyles includes:
|
|
160
|
-
// - font-family and other typography settings
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
### Inline Style Helpers
|
|
164
|
-
|
|
165
|
-
```typescript
|
|
166
|
-
import { inlineStyle, formatAttributes } from '@onivoro/server-html';
|
|
167
|
-
|
|
168
|
-
// Convert style object to inline style string
|
|
169
|
-
const styleString = inlineStyle({
|
|
170
|
-
'font-size': '16px',
|
|
171
|
-
color: '#333'
|
|
172
|
-
});
|
|
173
|
-
// Result: 'style="font-size:16px;color:#333"'
|
|
174
|
-
|
|
175
|
-
// Format attributes for HTML
|
|
176
|
-
const attrString = formatAttributes({
|
|
177
|
-
id: 'my-id',
|
|
178
|
-
'data-value': '123'
|
|
179
|
-
});
|
|
180
|
-
// Result: 'id="my-id" data-value="123"'
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
## Advanced Usage
|
|
184
|
-
|
|
185
|
-
### Custom Element Creation
|
|
186
|
-
|
|
187
|
-
```typescript
|
|
188
|
-
import { element, selfClosingElement } from '@onivoro/server-html';
|
|
189
|
-
|
|
190
|
-
// Create custom elements
|
|
191
|
-
const customElement = element('custom-tag', ['content'], {
|
|
192
|
-
'custom-attr': 'value'
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
// Create self-closing elements
|
|
196
|
-
const input = selfClosingElement('input', {
|
|
197
|
-
type: 'text',
|
|
198
|
-
name: 'username'
|
|
199
|
-
});
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### Complex Layouts
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
import { div, header, main, h1, p, button } from '@onivoro/server-html';
|
|
206
|
-
|
|
207
|
-
const pageLayout = div([
|
|
208
|
-
header([
|
|
209
|
-
h1(['My Application']),
|
|
210
|
-
], { cssClass: 'header' }),
|
|
211
|
-
|
|
212
|
-
main([
|
|
213
|
-
div([
|
|
214
|
-
h1(['Welcome']),
|
|
215
|
-
p(['This is the main content area.']),
|
|
216
|
-
button(['Get Started'], {
|
|
217
|
-
style: { 'background-color': '#28a745', color: 'white' }
|
|
218
|
-
})
|
|
219
|
-
], { cssClass: 'content' })
|
|
220
|
-
], { cssClass: 'main' })
|
|
221
|
-
], { cssClass: 'page-wrapper' });
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
## API Reference
|
|
225
|
-
|
|
226
|
-
### Element Builders
|
|
227
|
-
|
|
228
|
-
| Function | Description | Usage |
|
|
229
|
-
|----------|-------------|--------|
|
|
230
|
-
| `div(content, attrs?)` | Create a div element | `div(['content'])` |
|
|
231
|
-
| `h1-h6(content, attrs?)` | Create heading elements | `h1(['Title'])` |
|
|
232
|
-
| `p(content, attrs?)` | Create paragraph | `p(['Text'])` |
|
|
233
|
-
| `button(content, attrs?)` | Create button with built-in styles | `button(['Click'])` |
|
|
234
|
-
| `anchor(content, attrs?)` | Create anchor with button styles | `anchor(['Link'])` |
|
|
235
|
-
| `img(attrs)` | Create image element | `img({ src: 'url' })` |
|
|
236
|
-
|
|
237
|
-
### Table Elements
|
|
238
|
-
|
|
239
|
-
| Function | Description |
|
|
240
|
-
|----------|-------------|
|
|
241
|
-
| `tab(content, attrs?)` | Table wrapper |
|
|
242
|
-
| `thead(content, attrs?)` | Table header |
|
|
243
|
-
| `tbody(content, attrs?)` | Table body |
|
|
244
|
-
| `tr(content, attrs?)` | Table row |
|
|
245
|
-
| `th(content, attrs?)` | Table header cell |
|
|
246
|
-
| `td(content, attrs?)` | Table data cell |
|
|
247
|
-
|
|
248
|
-
### Utilities
|
|
249
|
-
|
|
250
|
-
| Function | Description |
|
|
251
|
-
|----------|-------------|
|
|
252
|
-
| `emailBody(title, subtitle, content, logo?, styles?)` | Email template |
|
|
253
|
-
| `table(columns, rows)` | Data table generator |
|
|
254
|
-
| `formatAttributes(attrs)` | Format HTML attributes |
|
|
255
|
-
| `inlineStyle(styles)` | Convert style object to string |
|
|
256
|
-
|
|
257
|
-
## Type Definitions
|
|
258
|
-
|
|
259
|
-
```typescript
|
|
260
|
-
type TAttributes = Record<string, any> & {
|
|
261
|
-
cssClass?: string;
|
|
262
|
-
style?: Record<string, string>;
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
type TElementRenderer = (
|
|
266
|
-
content: Array<string | number>,
|
|
267
|
-
attributes?: TAttributes
|
|
268
|
-
) => string;
|
|
269
|
-
|
|
270
|
-
type TSelfClosingElementRenderer = (
|
|
271
|
-
attributes?: TAttributes
|
|
272
|
-
) => string;
|
|
273
|
-
```
|
|
274
|
-
|
|
275
34
|
## Contributing
|
|
276
35
|
|
|
277
36
|
This package is part of the Onivoro monorepo. Please refer to the main repository for contribution guidelines.
|