1 | // Copyright (c) Jupyter Development Team.
|
2 | // Distributed under the terms of the Modified BSD License.
|
3 | import { Text } from '@jupyterlab/coreutils';
|
4 | /**
|
5 | * Inner works of class combining functions
|
6 | */
|
7 | function _classes(classes) {
|
8 | return classes
|
9 | .map(c => c && typeof c === 'object'
|
10 | ? Object.keys(c).map(key => !!c[key] && key)
|
11 | : typeof c === 'string'
|
12 | ? c.split(/\s+/)
|
13 | : [])
|
14 | .reduce((flattened, c) => flattened.concat(c), [])
|
15 | .filter(c => !!c);
|
16 | }
|
17 | /**
|
18 | * Combines classNames.
|
19 | *
|
20 | * @param classes - A list of classNames
|
21 | *
|
22 | * @returns A single string with the combined className
|
23 | */
|
24 | export function classes(...classes) {
|
25 | return _classes(classes).join(' ');
|
26 | }
|
27 | /**
|
28 | * Combines classNames. Removes all duplicates
|
29 | *
|
30 | * @param classes - A list of classNames
|
31 | *
|
32 | * @returns A single string with the combined className
|
33 | */
|
34 | export function classesDedupe(...classes) {
|
35 | return [...new Set(_classes(classes))].join(' ');
|
36 | }
|
37 | /**
|
38 | * Translates the attributes of a DOM element into attributes that can
|
39 | * be understood by React. Currently not comprehensive, we will add special
|
40 | * cases as they become relevant.
|
41 | *
|
42 | * @param elem - A DOM element
|
43 | *
|
44 | * @param ignore - An optional list of attribute names to ignore
|
45 | *
|
46 | * @returns An object with key:value pairs that are the React-friendly
|
47 | * translation of elem's attributes
|
48 | */
|
49 | export function getReactAttrs(elem, { ignore = [] } = {}) {
|
50 | return elem.getAttributeNames().reduce((d, name) => {
|
51 | if (name === 'style' || ignore.includes(name)) {
|
52 | void 0;
|
53 | }
|
54 | else if (name.startsWith('data')) {
|
55 | d[name] = elem.getAttribute(name);
|
56 | }
|
57 | else {
|
58 | d[Text.camelCase(name)] = elem.getAttribute(name);
|
59 | }
|
60 | return d;
|
61 | }, {});
|
62 | }
|
63 | //# sourceMappingURL=utils.js.map |
\ | No newline at end of file |