UNPKG

3.1 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { caretDownEmptyIcon } from '@jupyterlab/ui-components';
4/**
5 * A namespace for node styling.
6 */
7export var Styling;
8(function (Styling) {
9 /**
10 * Style a node and its child elements with the default tag names.
11 *
12 * @param node - The base node.
13 *
14 * @param className - The optional CSS class to add to styled nodes.
15 */
16 function styleNode(node, className = '') {
17 styleNodeByTag(node, 'select', className);
18 styleNodeByTag(node, 'textarea', className);
19 styleNodeByTag(node, 'input', className);
20 styleNodeByTag(node, 'button', className);
21 }
22 Styling.styleNode = styleNode;
23 /**
24 * Style a node and its elements that have a given tag name.
25 *
26 * @param node - The base node.
27 *
28 * @param tagName - The html tag name to style.
29 *
30 * @param className - The optional CSS class to add to styled nodes.
31 */
32 function styleNodeByTag(node, tagName, className = '') {
33 if (node.localName === tagName) {
34 node.classList.add('jp-mod-styled');
35 }
36 if (node.localName === 'select') {
37 wrapSelect(node);
38 }
39 const nodes = node.getElementsByTagName(tagName);
40 for (let i = 0; i < nodes.length; i++) {
41 const child = nodes[i];
42 child.classList.add('jp-mod-styled');
43 if (className) {
44 child.classList.add(className);
45 }
46 if (tagName === 'select') {
47 wrapSelect(child);
48 }
49 }
50 }
51 Styling.styleNodeByTag = styleNodeByTag;
52 /**
53 * Wrap a select node.
54 */
55 function wrapSelect(node) {
56 const wrapper = document.createElement('div');
57 wrapper.classList.add('jp-select-wrapper');
58 node.addEventListener('focus', Private.onFocus);
59 node.addEventListener('blur', Private.onFocus);
60 node.classList.add('jp-mod-styled');
61 if (node.parentElement) {
62 node.parentElement.replaceChild(wrapper, node);
63 }
64 wrapper.appendChild(node);
65 // add the icon node
66 wrapper.appendChild(caretDownEmptyIcon.element({
67 tag: 'span',
68 stylesheet: 'select',
69 right: '8px',
70 top: '5px',
71 width: '18px'
72 }));
73 return wrapper;
74 }
75 Styling.wrapSelect = wrapSelect;
76})(Styling || (Styling = {}));
77/**
78 * The namespace for module private data.
79 */
80var Private;
81(function (Private) {
82 /**
83 * Handle a focus event on a styled select.
84 */
85 function onFocus(event) {
86 const target = event.target;
87 const parent = target.parentElement;
88 if (!parent) {
89 return;
90 }
91 if (event.type === 'focus') {
92 parent.classList.add('jp-mod-focused');
93 }
94 else {
95 parent.classList.remove('jp-mod-focused');
96 }
97 }
98 Private.onFocus = onFocus;
99})(Private || (Private = {}));
100//# sourceMappingURL=styling.js.map
\No newline at end of file