UNPKG

12.1 kBTypeScriptView Raw
1export { thunk, Thunk, ThunkData } from './thunk';
2export { VNode, VNodeData } from 'snabbdom/vnode';
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/h';
174import { HyperScriptHelperFn, SVGHelperFn } from './hyperscript-helpers';
175export declare const svg: SVGHelperFn;
176export declare const a: HyperScriptHelperFn;
177export declare const abbr: HyperScriptHelperFn;
178export declare const address: HyperScriptHelperFn;
179export declare const area: HyperScriptHelperFn;
180export declare const article: HyperScriptHelperFn;
181export declare const aside: HyperScriptHelperFn;
182export declare const audio: HyperScriptHelperFn;
183export declare const b: HyperScriptHelperFn;
184export declare const base: HyperScriptHelperFn;
185export declare const bdi: HyperScriptHelperFn;
186export declare const bdo: HyperScriptHelperFn;
187export declare const blockquote: HyperScriptHelperFn;
188export declare const body: HyperScriptHelperFn;
189export declare const br: HyperScriptHelperFn;
190export declare const button: HyperScriptHelperFn;
191export declare const canvas: HyperScriptHelperFn;
192export declare const caption: HyperScriptHelperFn;
193export declare const cite: HyperScriptHelperFn;
194export declare const code: HyperScriptHelperFn;
195export declare const col: HyperScriptHelperFn;
196export declare const colgroup: HyperScriptHelperFn;
197export declare const dd: HyperScriptHelperFn;
198export declare const del: HyperScriptHelperFn;
199export declare const dfn: HyperScriptHelperFn;
200export declare const dir: HyperScriptHelperFn;
201export declare const div: HyperScriptHelperFn;
202export declare const dl: HyperScriptHelperFn;
203export declare const dt: HyperScriptHelperFn;
204export declare const em: HyperScriptHelperFn;
205export declare const embed: HyperScriptHelperFn;
206export declare const fieldset: HyperScriptHelperFn;
207export declare const figcaption: HyperScriptHelperFn;
208export declare const figure: HyperScriptHelperFn;
209export declare const footer: HyperScriptHelperFn;
210export declare const form: HyperScriptHelperFn;
211export declare const h1: HyperScriptHelperFn;
212export declare const h2: HyperScriptHelperFn;
213export declare const h3: HyperScriptHelperFn;
214export declare const h4: HyperScriptHelperFn;
215export declare const h5: HyperScriptHelperFn;
216export declare const h6: HyperScriptHelperFn;
217export declare const head: HyperScriptHelperFn;
218export declare const header: HyperScriptHelperFn;
219export declare const hgroup: HyperScriptHelperFn;
220export declare const hr: HyperScriptHelperFn;
221export declare const html: HyperScriptHelperFn;
222export declare const i: HyperScriptHelperFn;
223export declare const iframe: HyperScriptHelperFn;
224export declare const img: HyperScriptHelperFn;
225export declare const input: HyperScriptHelperFn;
226export declare const ins: HyperScriptHelperFn;
227export declare const kbd: HyperScriptHelperFn;
228export declare const keygen: HyperScriptHelperFn;
229export declare const label: HyperScriptHelperFn;
230export declare const legend: HyperScriptHelperFn;
231export declare const li: HyperScriptHelperFn;
232export declare const link: HyperScriptHelperFn;
233export declare const main: HyperScriptHelperFn;
234export declare const map: HyperScriptHelperFn;
235export declare const mark: HyperScriptHelperFn;
236export declare const menu: HyperScriptHelperFn;
237export declare const meta: HyperScriptHelperFn;
238export declare const nav: HyperScriptHelperFn;
239export declare const noscript: HyperScriptHelperFn;
240export declare const object: HyperScriptHelperFn;
241export declare const ol: HyperScriptHelperFn;
242export declare const optgroup: HyperScriptHelperFn;
243export declare const option: HyperScriptHelperFn;
244export declare const p: HyperScriptHelperFn;
245export declare const param: HyperScriptHelperFn;
246export declare const pre: HyperScriptHelperFn;
247export declare const progress: HyperScriptHelperFn;
248export declare const q: HyperScriptHelperFn;
249export declare const rp: HyperScriptHelperFn;
250export declare const rt: HyperScriptHelperFn;
251export declare const ruby: HyperScriptHelperFn;
252export declare const s: HyperScriptHelperFn;
253export declare const samp: HyperScriptHelperFn;
254export declare const script: HyperScriptHelperFn;
255export declare const section: HyperScriptHelperFn;
256export declare const select: HyperScriptHelperFn;
257export declare const small: HyperScriptHelperFn;
258export declare const source: HyperScriptHelperFn;
259export declare const span: HyperScriptHelperFn;
260export declare const strong: HyperScriptHelperFn;
261export declare const style: HyperScriptHelperFn;
262export declare const sub: HyperScriptHelperFn;
263export declare const sup: HyperScriptHelperFn;
264export declare const table: HyperScriptHelperFn;
265export declare const tbody: HyperScriptHelperFn;
266export declare const td: HyperScriptHelperFn;
267export declare const textarea: HyperScriptHelperFn;
268export declare const tfoot: HyperScriptHelperFn;
269export declare const th: HyperScriptHelperFn;
270export declare const thead: HyperScriptHelperFn;
271export declare const title: HyperScriptHelperFn;
272export declare const tr: HyperScriptHelperFn;
273export declare const u: HyperScriptHelperFn;
274export declare const ul: HyperScriptHelperFn;
275export declare const video: HyperScriptHelperFn;