@divkitframework/divkit 3.0.0

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 ADDED
@@ -0,0 +1,282 @@
1
+ ## DivKit for the Web
2
+
3
+ ### Installation
4
+
5
+ ```
6
+ npm i @divkit/divkit --save
7
+ ```
8
+
9
+ ### Usage
10
+
11
+ [Example usage repos](../divkit-examples)
12
+
13
+ For all variants of usage, css file `dist/client.css` is required. Include it in any way (import as module, link directly in html, etc).
14
+
15
+ JS code can be bundled with various strategies. Basically you need to answer a few questions
16
+
17
+ ### Is there any server-side rendering (SSR) or will it be only on client?
18
+
19
+ #### SSR + hydration
20
+
21
+ On the server side there is `/server` module:
22
+
23
+ ```js
24
+ import {render} from '@divkit/divkit/server';
25
+
26
+ const html = render({
27
+ id: 'smth',
28
+ json: {
29
+ card: {},
30
+ templates: {}
31
+ },
32
+ onError(details) {
33
+ console.error(details.error);
34
+ }
35
+ });
36
+ ```
37
+
38
+ Then use `/client-hydratable` on client to hydrate server-side html::
39
+
40
+ ```js
41
+ import {render} from '@divkit/divkit/client-hydratable';
42
+
43
+ render({
44
+ id: 'smth',
45
+ target: document.querySelector('#root'),
46
+ hydrate: true,
47
+ json: {
48
+ card: {},
49
+ templates: {}
50
+ },
51
+ onError(details) {
52
+ console.error(details.error);
53
+ }
54
+ });
55
+ ```
56
+
57
+ #### Client-only rendering
58
+
59
+ For the client-only usage there is `/client` module. The size of this module is slightly smaller than `/client-hydratable`.
60
+
61
+ ```js
62
+ import {render} from '@divkit/divkit/client';
63
+
64
+ render({
65
+ id: 'smth',
66
+ target: document.querySelector('#root'),
67
+ json: {
68
+ card: {},
69
+ templates: {}
70
+ },
71
+ onError(details) {
72
+ console.error(details.error);
73
+ }
74
+ });
75
+ ```
76
+
77
+ ### Are you want to use ES module or CommonJS module?
78
+
79
+ The package contains both of them.
80
+
81
+ Node.js will automatically use the appropriate version.
82
+
83
+ Webpack will use ES modules version.
84
+
85
+ For the direct CommonJS usage, this files can be used:
86
+
87
+ ```
88
+ dist/client.js
89
+ dist/client-hydratable.js
90
+ dist/server.js
91
+ ```
92
+
93
+ ES modules files:
94
+
95
+ ```
96
+ dist/esm/client.mjs
97
+ dist/esm/client-hydratable.mjs
98
+ dist/esm/server.mjs
99
+ ```
100
+
101
+ ES modules can be used in the browser directly without any build:
102
+
103
+ ```html
104
+ <script type="module">
105
+ import {render} from './node_modules/@divkit/divkit/dist/esm/client.mjs';
106
+
107
+ render({
108
+ id: 'smth',
109
+ target: document.querySelector('#root'),
110
+ json: {
111
+ card: {},
112
+ templates: {}
113
+ },
114
+ onError(details) {
115
+ console.error(details.error);
116
+ }
117
+ });
118
+ </script>
119
+ ```
120
+
121
+ ### Use in the browser via global variables without build:
122
+
123
+ ```html
124
+ <script src="./node_modules/@divkit/divkit/dist/browser/client.js"></script>
125
+ <script>
126
+ window.Ya.Divkit.render({
127
+ id: 'smth',
128
+ target: document.querySelector('#root'),
129
+ json: {
130
+ card: {},
131
+ templates: {}
132
+ },
133
+ onError(details) {
134
+ console.error(details.error);
135
+ }
136
+ });
137
+ </script>
138
+ ```
139
+
140
+ ### TypeScript and types
141
+
142
+ All modules have typescript definitions (client, client-hydratable and server), so typescript will load them at any use.
143
+
144
+ ### Browser/Node.js support
145
+
146
+ Browser support
147
+ ```
148
+ chrome >= 58
149
+ safari >= 11
150
+ firefox >= 67
151
+ ```
152
+
153
+ Node.js
154
+ ```
155
+ Node.js >= 8
156
+ ```
157
+
158
+ ### API
159
+
160
+ All 3 exported modules have an exported function `render`. This function works in a similar way on client and server.
161
+
162
+ `/client` and `/client-hydratable` requires option `target` - an HTML-element that is used to render json.
163
+
164
+ Instead, `/server` module will return an HTML string.
165
+
166
+ ### Options
167
+
168
+ #### id
169
+
170
+ String, required.
171
+
172
+ Means the unique block identifier. Used to generate ids and classes. There should not be 2 blocks with the same `id` on the page.
173
+
174
+ #### json
175
+
176
+ Object, required.
177
+
178
+ Divjson itself.
179
+
180
+ #### target
181
+
182
+ `/client` and `/client-hydratable`
183
+
184
+ HTML-element, required.
185
+
186
+ #### hydrate
187
+
188
+ `/client-hydratable`
189
+
190
+ Boolean, optional.
191
+
192
+ It must be `true`, if the current render must hydrate html in `target`.
193
+
194
+ #### onError
195
+
196
+ Function, optional.
197
+
198
+ Callback for errors and warnings for manual processing.
199
+
200
+ ```js
201
+ function onError({error}) {
202
+ console.log(error.level, error.additional, error.message);
203
+ }
204
+ ```
205
+
206
+ #### onStat
207
+
208
+ `/client` and `/client-hydratable`
209
+
210
+ Function, optional.
211
+
212
+ Used for logging clicks (for elements with `action`) and visibility logging (for elements with `visibility_action`).
213
+
214
+ ```js
215
+ function onStat(details) {
216
+ // details.type: 'click' | 'visible'
217
+ // details.action: action | visibility_action
218
+ }
219
+ ```
220
+
221
+ #### platform
222
+
223
+ `desktop` | `touch` | `auto`
224
+
225
+ The default value is `auto`. Tweaks for mouse or touch events.
226
+
227
+
228
+ #### theme (EXPERIMENTAL)
229
+
230
+ `system` | `light` | `dark`
231
+
232
+ The default value is `system`. Affects variables in `palette`.
233
+
234
+
235
+ ### Palette support (EXPERIMENTAL)
236
+
237
+ Divjson along with the `card` and `templates` can contain a `palette` property with colors for light and dark themes:
238
+
239
+ ```json
240
+ {
241
+ "card": {
242
+ "states": [
243
+ {
244
+ "div": {
245
+ "type": "text",
246
+ "text": "Hello palette",
247
+ "text_color": "@{text}",
248
+ "background": [{
249
+ "type": "solid",
250
+ "color": "@{bg}"
251
+ }]
252
+ },
253
+ "state_id": 0
254
+ }
255
+ ],
256
+ "log_id": "test"
257
+ },
258
+ "templates": {},
259
+ "palette": {
260
+ "dark": [
261
+ {
262
+ "name": "bg",
263
+ "color": "#000"
264
+ },
265
+ {
266
+ "name": "text",
267
+ "color": "#fff"
268
+ }
269
+ ],
270
+ "light": [
271
+ {
272
+ "name": "bg",
273
+ "color": "#fff"
274
+ },
275
+ {
276
+ "name": "text",
277
+ "color": "#000"
278
+ }
279
+ ]
280
+ }
281
+ }
282
+ ```
@@ -0,0 +1,6 @@
1
+ {
2
+ "internal": true,
3
+ "main": "../dist/client.js",
4
+ "module": "../dist/esm/client.mjs",
5
+ "types": "../typings/client.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "internal": true,
3
+ "main": "../dist/client-devtool.js",
4
+ "module": "../dist/esm/client-devtool.mjs",
5
+ "types": "../typings/client-devtool.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "internal": true,
3
+ "main": "../dist/client-hydratable.js",
4
+ "module": "../dist/esm/client-hydratable.mjs",
5
+ "types": "../typings/client-hydratable.d.ts"
6
+ }