UNPKG

12.3 kBPlain TextView Raw
1export {thunk, Thunk, ThunkData} from './thunk';
2export {VNode, VNodeData} from 'snabbdom';
3export {DOMSource, EventsFnOptions} from './DOMSource';
4export {MainDOMSource} from './MainDOMSource';
5/**
6 * A factory for the DOM driver function.
7 *
8 * Takes a `container` to define the target on the existing DOM which this
9 * driver will operate on, and an `options` object as the second argument. The
10 * input to this driver is a stream of virtual DOM objects, or in other words,
11 * Snabbdom "VNode" objects. The output of this driver is a "DOMSource": a
12 * collection of Observables queried with the methods `select()` and `events()`.
13 *
14 * **`DOMSource.select(selector)`** returns a new DOMSource with scope
15 * restricted to the element(s) that matches the CSS `selector` given. To select
16 * the page's `document`, use `.select('document')`. To select the container
17 * element for this app, use `.select(':root')`.
18 *
19 * **`DOMSource.events(eventType, options)`** returns a stream of events of
20 * `eventType` happening on the elements that match the current DOMSource. The
21 * event object contains the `ownerTarget` property that behaves exactly like
22 * `currentTarget`. The reason for this is that some browsers doesn't allow
23 * `currentTarget` property to be mutated, hence a new property is created. The
24 * returned stream is an *xstream* Stream if you use `@cycle/xstream-run` to run
25 * your app with this driver, or it is an RxJS Observable if you use
26 * `@cycle/rxjs-run`, and so forth.
27 *
28 * **options for DOMSource.events**
29 *
30 * The `options` parameter on `DOMSource.events(eventType, options)` is an
31 * (optional) object with two optional fields: `useCapture` and
32 * `preventDefault`.
33 *
34 * `useCapture` is by default `false`, except it is `true` for event types that
35 * do not bubble. Read more here
36 * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
37 * about the `useCapture` and its purpose.
38 *
39 * `preventDefault` is by default `false`, and indicates to the driver whether
40 * `event.preventDefault()` should be invoked. This option can be configured in
41 * three ways:
42 *
43 * - `{preventDefault: boolean}` to invoke preventDefault if `true`, and not
44 * invoke otherwise.
45 * - `{preventDefault: (ev: Event) => boolean}` for conditional invocation.
46 * - `{preventDefault: NestedObject}` uses an object to be recursively compared
47 * to the `Event` object. `preventDefault` is invoked when all properties on the
48 * nested object match with the properties on the event object.
49 *
50 * Here are some examples:
51 * ```typescript
52 * // always prevent default
53 * DOMSource.select('input').events('keydown', {
54 * preventDefault: true
55 * })
56 *
57 * // prevent default only when `ENTER` is pressed
58 * DOMSource.select('input').events('keydown', {
59 * preventDefault: e => e.keyCode === 13
60 * })
61 *
62 * // prevent defualt when `ENTER` is pressed AND target.value is 'HELLO'
63 * DOMSource.select('input').events('keydown', {
64 * preventDefault: { keyCode: 13, ownerTarget: { value: 'HELLO' } }
65 * });
66 * ```
67 *
68 * **`DOMSource.elements()`** returns a stream of arrays containing the DOM
69 * elements that match the selectors in the DOMSource (e.g. from previous
70 * `select(x)` calls).
71 *
72 * **`DOMSource.element()`** returns a stream of DOM elements. Notice that this
73 * is the singular version of `.elements()`, so the stream will emit an element,
74 * not an array. If there is no element that matches the selected DOMSource,
75 * then the returned stream will not emit anything.
76 *
77 * @param {(String|HTMLElement)} container the DOM selector for the element
78 * (or the element itself) to contain the rendering of the VTrees.
79 * @param {DOMDriverOptions} options an object with two optional properties:
80 *
81 * - `modules: array` overrides `@cycle/dom`'s default Snabbdom modules as
82 * as defined in [`src/modules.ts`](./src/modules.ts).
83 * - `reportSnabbdomError: (err: any) => void` overrides the default error reporter function.
84 * @return {Function} the DOM driver function. The function expects a stream of
85 * VNode as input, and outputs the DOMSource object.
86 * @function makeDOMDriver
87 */
88export {makeDOMDriver, DOMDriverOptions} from './makeDOMDriver';
89/**
90 * A factory function to create mocked DOMSource objects, for testing purposes.
91 *
92 * Takes a `mockConfig` object as argument, and returns
93 * a DOMSource that can be given to any Cycle.js app that expects a DOMSource in
94 * the sources, for testing.
95 *
96 * The `mockConfig` parameter is an object specifying selectors, eventTypes and
97 * their streams. Example:
98 *
99 * ```js
100 * const domSource = mockDOMSource({
101 * '.foo': {
102 * 'click': xs.of({target: {}}),
103 * 'mouseover': xs.of({target: {}}),
104 * },
105 * '.bar': {
106 * 'scroll': xs.of({target: {}}),
107 * elements: xs.of({tagName: 'div'}),
108 * }
109 * });
110 *
111 * // Usage
112 * const click$ = domSource.select('.foo').events('click');
113 * const element$ = domSource.select('.bar').elements();
114 * ```
115 *
116 * The mocked DOM Source supports isolation. It has the functions `isolateSink`
117 * and `isolateSource` attached to it, and performs simple isolation using
118 * classNames. *isolateSink* with scope `foo` will append the class `___foo` to
119 * the stream of virtual DOM nodes, and *isolateSource* with scope `foo` will
120 * perform a conventional `mockedDOMSource.select('.__foo')` call.
121 *
122 * @param {Object} mockConfig an object where keys are selector strings
123 * and values are objects. Those nested objects have `eventType` strings as keys
124 * and values are streams you created.
125 * @return {Object} fake DOM source object, with an API containing `select()`
126 * and `events()` and `elements()` which can be used just like the DOM Driver's
127 * DOMSource.
128 *
129 * @function mockDOMSource
130 */
131export {mockDOMSource, MockConfig, MockedDOMSource} from './mockDOMSource';
132export {CycleDOMEvent} from './EventDelegator';
133/**
134 * The hyperscript function `h()` is a function to create virtual DOM objects,
135 * also known as VNodes. Call
136 *
137 * ```js
138 * h('div.myClass', {style: {color: 'red'}}, [])
139 * ```
140 *
141 * to create a VNode that represents a `DIV` element with className `myClass`,
142 * styled with red color, and no children because the `[]` array was passed. The
143 * API is `h(tagOrSelector, optionalData, optionalChildrenOrText)`.
144 *
145 * However, usually you should use "hyperscript helpers", which are shortcut
146 * functions based on hyperscript. There is one hyperscript helper function for
147 * each DOM tagName, such as `h1()`, `h2()`, `div()`, `span()`, `label()`,
148 * `input()`. For instance, the previous example could have been written
149 * as:
150 *
151 * ```js
152 * div('.myClass', {style: {color: 'red'}}, [])
153 * ```
154 *
155 * There are also SVG helper functions, which apply the appropriate SVG
156 * namespace to the resulting elements. `svg()` function creates the top-most
157 * SVG element, and `svg.g`, `svg.polygon`, `svg.circle`, `svg.path` are for
158 * SVG-specific child elements. Example:
159 *
160 * ```js
161 * svg({attrs: {width: 150, height: 150}}, [
162 * svg.polygon({
163 * attrs: {
164 * class: 'triangle',
165 * points: '20 0 20 150 150 20'
166 * }
167 * })
168 * ])
169 * ```
170 *
171 * @function h
172 */
173export {h} from 'snabbdom';
174import hh, {HyperScriptHelperFn, SVGHelperFn} from './hyperscript-helpers';
175
176export const svg: SVGHelperFn = hh.svg;
177export const a: HyperScriptHelperFn = hh.a;
178export const abbr: HyperScriptHelperFn = hh.abbr;
179export const address: HyperScriptHelperFn = hh.address;
180export const area: HyperScriptHelperFn = hh.area;
181export const article: HyperScriptHelperFn = hh.article;
182export const aside: HyperScriptHelperFn = hh.aside;
183export const audio: HyperScriptHelperFn = hh.audio;
184export const b: HyperScriptHelperFn = hh.b;
185export const base: HyperScriptHelperFn = hh.base;
186export const bdi: HyperScriptHelperFn = hh.bdi;
187export const bdo: HyperScriptHelperFn = hh.bdo;
188export const blockquote: HyperScriptHelperFn = hh.blockquote;
189export const body: HyperScriptHelperFn = hh.body;
190export const br: HyperScriptHelperFn = hh.br;
191export const button: HyperScriptHelperFn = hh.button;
192export const canvas: HyperScriptHelperFn = hh.canvas;
193export const caption: HyperScriptHelperFn = hh.caption;
194export const cite: HyperScriptHelperFn = hh.cite;
195export const code: HyperScriptHelperFn = hh.code;
196export const col: HyperScriptHelperFn = hh.col;
197export const colgroup: HyperScriptHelperFn = hh.colgroup;
198export const dd: HyperScriptHelperFn = hh.dd;
199export const del: HyperScriptHelperFn = hh.del;
200export const dfn: HyperScriptHelperFn = hh.dfn;
201export const dir: HyperScriptHelperFn = hh.dir;
202export const div: HyperScriptHelperFn = hh.div;
203export const dl: HyperScriptHelperFn = hh.dl;
204export const dt: HyperScriptHelperFn = hh.dt;
205export const em: HyperScriptHelperFn = hh.em;
206export const embed: HyperScriptHelperFn = hh.embed;
207export const fieldset: HyperScriptHelperFn = hh.fieldset;
208export const figcaption: HyperScriptHelperFn = hh.figcaption;
209export const figure: HyperScriptHelperFn = hh.figure;
210export const footer: HyperScriptHelperFn = hh.footer;
211export const form: HyperScriptHelperFn = hh.form;
212export const h1: HyperScriptHelperFn = hh.h1;
213export const h2: HyperScriptHelperFn = hh.h2;
214export const h3: HyperScriptHelperFn = hh.h3;
215export const h4: HyperScriptHelperFn = hh.h4;
216export const h5: HyperScriptHelperFn = hh.h5;
217export const h6: HyperScriptHelperFn = hh.h6;
218export const head: HyperScriptHelperFn = hh.head;
219export const header: HyperScriptHelperFn = hh.header;
220export const hgroup: HyperScriptHelperFn = hh.hgroup;
221export const hr: HyperScriptHelperFn = hh.hr;
222export const html: HyperScriptHelperFn = hh.html;
223export const i: HyperScriptHelperFn = hh.i;
224export const iframe: HyperScriptHelperFn = hh.iframe;
225export const img: HyperScriptHelperFn = hh.img;
226export const input: HyperScriptHelperFn = hh.input;
227export const ins: HyperScriptHelperFn = hh.ins;
228export const kbd: HyperScriptHelperFn = hh.kbd;
229export const keygen: HyperScriptHelperFn = hh.keygen;
230export const label: HyperScriptHelperFn = hh.label;
231export const legend: HyperScriptHelperFn = hh.legend;
232export const li: HyperScriptHelperFn = hh.li;
233export const link: HyperScriptHelperFn = hh.link;
234export const main: HyperScriptHelperFn = hh.main;
235export const map: HyperScriptHelperFn = hh.map;
236export const mark: HyperScriptHelperFn = hh.mark;
237export const menu: HyperScriptHelperFn = hh.menu;
238export const meta: HyperScriptHelperFn = hh.meta;
239export const nav: HyperScriptHelperFn = hh.nav;
240export const noscript: HyperScriptHelperFn = hh.noscript;
241export const object: HyperScriptHelperFn = hh.object;
242export const ol: HyperScriptHelperFn = hh.ol;
243export const optgroup: HyperScriptHelperFn = hh.optgroup;
244export const option: HyperScriptHelperFn = hh.option;
245export const p: HyperScriptHelperFn = hh.p;
246export const param: HyperScriptHelperFn = hh.param;
247export const pre: HyperScriptHelperFn = hh.pre;
248export const progress: HyperScriptHelperFn = hh.progress;
249export const q: HyperScriptHelperFn = hh.q;
250export const rp: HyperScriptHelperFn = hh.rp;
251export const rt: HyperScriptHelperFn = hh.rt;
252export const ruby: HyperScriptHelperFn = hh.ruby;
253export const s: HyperScriptHelperFn = hh.s;
254export const samp: HyperScriptHelperFn = hh.samp;
255export const script: HyperScriptHelperFn = hh.script;
256export const section: HyperScriptHelperFn = hh.section;
257export const select: HyperScriptHelperFn = hh.select;
258export const small: HyperScriptHelperFn = hh.small;
259export const source: HyperScriptHelperFn = hh.source;
260export const span: HyperScriptHelperFn = hh.span;
261export const strong: HyperScriptHelperFn = hh.strong;
262export const style: HyperScriptHelperFn = hh.style;
263export const sub: HyperScriptHelperFn = hh.sub;
264export const sup: HyperScriptHelperFn = hh.sup;
265export const table: HyperScriptHelperFn = hh.table;
266export const tbody: HyperScriptHelperFn = hh.tbody;
267export const td: HyperScriptHelperFn = hh.td;
268export const textarea: HyperScriptHelperFn = hh.textarea;
269export const tfoot: HyperScriptHelperFn = hh.tfoot;
270export const th: HyperScriptHelperFn = hh.th;
271export const thead: HyperScriptHelperFn = hh.thead;
272export const title: HyperScriptHelperFn = hh.title;
273export const tr: HyperScriptHelperFn = hh.tr;
274export const u: HyperScriptHelperFn = hh.u;
275export const ul: HyperScriptHelperFn = hh.ul;
276export const video: HyperScriptHelperFn = hh.video;