@dxos/ui 0.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/LICENSE +8 -0
- package/README.md +21 -0
- package/package.json +37 -0
- package/src/device.ts +8 -0
- package/src/domino.test.ts +75 -0
- package/src/domino.ts +80 -0
- package/src/index.ts +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2022 DXOS
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @dxos/ui
|
|
2
|
+
|
|
3
|
+
Low-level UI components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm i @dxos/ui
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## DXOS Resources
|
|
12
|
+
|
|
13
|
+
- [Website](https://dxos.org)
|
|
14
|
+
- [Developer Documentation](https://docs.dxos.org)
|
|
15
|
+
- Talk to us on [Discord](https://dxos.org/discord)
|
|
16
|
+
|
|
17
|
+
## Contributions
|
|
18
|
+
|
|
19
|
+
Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
|
|
20
|
+
|
|
21
|
+
License: [MIT](./LICENSE) Copyright 2022 © DXOS
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/ui",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Core UI.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "DXOS.org",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"source": "./src/index.ts",
|
|
14
|
+
"types": "./dist/types/src/index.d.ts",
|
|
15
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
16
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "dist/types/src/index.d.ts",
|
|
20
|
+
"typesVersions": {
|
|
21
|
+
"*": {}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"src"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@dxos/ui-theme": "0.0.0",
|
|
29
|
+
"@dxos/ui-types": "0.0.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"vitest-ui": "vitest --ui"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/device.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export const hasIosKeyboard = () => {
|
|
6
|
+
// TODO(thure): UA sniffing is never good, however I haven’t found a better way to query for whether the on-screen keyboard will appear on `focus` outside of a click handler.
|
|
7
|
+
return !!navigator.userAgent.match(/(iPad|iPod|iPhone).+Safari/);
|
|
8
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { mx } from '@dxos/ui-theme';
|
|
8
|
+
|
|
9
|
+
import { Domino } from './domino';
|
|
10
|
+
|
|
11
|
+
describe('domino', () => {
|
|
12
|
+
test('classNames uses mx for class merging', () => {
|
|
13
|
+
const el = Domino.of('div').classNames('foo bar').root;
|
|
14
|
+
|
|
15
|
+
// Simple string.
|
|
16
|
+
expect(el.classList.contains('foo')).toBe(true);
|
|
17
|
+
expect(el.classList.contains('bar')).toBe(true);
|
|
18
|
+
|
|
19
|
+
// Using mx directly - should merge conflicting Tailwind classes.
|
|
20
|
+
const el2 = Domino.of('div').classNames(mx('p-4', 'p-2')).root; // p-2 should win.
|
|
21
|
+
expect(el2.classList.contains('p-2')).toBe(true);
|
|
22
|
+
expect(el2.classList.contains('p-4')).toBe(false);
|
|
23
|
+
|
|
24
|
+
// Array with conditionals.
|
|
25
|
+
const isActive = true;
|
|
26
|
+
const isDisabled = false;
|
|
27
|
+
const el3 = Domino.of('div').classNames(['base-class', isActive && 'active', isDisabled && 'disabled']).root;
|
|
28
|
+
expect(el3.classList.contains('base-class')).toBe(true);
|
|
29
|
+
expect(el3.classList.contains('active')).toBe(true);
|
|
30
|
+
expect(el3.classList.contains('disabled')).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('Domino builds HTML elements', () => {
|
|
34
|
+
const el = Domino.of('div').text('Hello').classNames('test-class').root;
|
|
35
|
+
|
|
36
|
+
expect(el.tagName.toLowerCase()).toBe('div');
|
|
37
|
+
expect(el.textContent).toBe('Hello');
|
|
38
|
+
expect(el.classList.contains('test-class')).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('Domino adds attributes', () => {
|
|
42
|
+
const el = Domino.of('input').attributes({ type: 'text', placeholder: 'Enter text' }).root;
|
|
43
|
+
|
|
44
|
+
expect(el.getAttribute('type')).toBe('text');
|
|
45
|
+
expect(el.getAttribute('placeholder')).toBe('Enter text');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('Domino adds data attributes', () => {
|
|
49
|
+
const el = Domino.of('div').data('testId', '123').root;
|
|
50
|
+
|
|
51
|
+
expect((el as HTMLElement).dataset.testId).toBe('123');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('Domino adds event listeners', () => {
|
|
55
|
+
let clicked = false;
|
|
56
|
+
const el = Domino.of('button').on('click', () => {
|
|
57
|
+
clicked = true;
|
|
58
|
+
}).root;
|
|
59
|
+
|
|
60
|
+
el.click();
|
|
61
|
+
expect(clicked).toBe(true);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('Domino creates an SVG element', () => {
|
|
65
|
+
const el = Domino.of('circle', Domino.SVG).root;
|
|
66
|
+
// Note: happy-dom returns uppercase tagName even for createElementNS in some versions/contexts.
|
|
67
|
+
expect(el.tagName.toLowerCase()).toBe('circle');
|
|
68
|
+
expect(el.namespaceURI).toBe('http://www.w3.org/2000/svg');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('Domino creates nested SVG elements', () => {
|
|
72
|
+
const svg = Domino.of('svg', Domino.SVG).children(Domino.of('circle', Domino.SVG)).root;
|
|
73
|
+
expect(svg.querySelector('circle')).toBeTruthy();
|
|
74
|
+
});
|
|
75
|
+
});
|
package/src/domino.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { mx } from '@dxos/ui-theme';
|
|
6
|
+
import { type ClassNameValue } from '@dxos/ui-types';
|
|
7
|
+
|
|
8
|
+
// From icon-plugin.
|
|
9
|
+
const ICONS_URL = '/icons.svg';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Super lightweight chainable DOM builder.
|
|
13
|
+
*/
|
|
14
|
+
export class Domino<T extends HTMLElement | SVGElement> {
|
|
15
|
+
static SVG = 'http://www.w3.org/2000/svg';
|
|
16
|
+
|
|
17
|
+
static icon = (icon: string) => ICONS_URL + '#' + icon;
|
|
18
|
+
|
|
19
|
+
static of<K extends keyof HTMLElementTagNameMap>(tag: K): Domino<HTMLElementTagNameMap[K]>;
|
|
20
|
+
static of<K extends keyof SVGElementTagNameMap>(tag: K, namespace: string): Domino<SVGElementTagNameMap[K]>;
|
|
21
|
+
static of(tag: string, namespace?: string): Domino<HTMLElement | SVGElement> {
|
|
22
|
+
return new Domino(tag, namespace);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private readonly _el: T;
|
|
26
|
+
|
|
27
|
+
private constructor(tag: string, namespace?: string) {
|
|
28
|
+
if (namespace) {
|
|
29
|
+
this._el = document.createElementNS(namespace, tag) as T;
|
|
30
|
+
} else {
|
|
31
|
+
this._el = document.createElement(tag) as T;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
classNames(...classNames: ClassNameValue[]): this {
|
|
36
|
+
const merged = mx(classNames);
|
|
37
|
+
if (this._el instanceof HTMLElement || this._el instanceof SVGElement) {
|
|
38
|
+
this._el.setAttribute('class', merged);
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
text(value: string): this {
|
|
44
|
+
this._el.textContent = value;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
data(key: string, value: string): this {
|
|
49
|
+
if (this._el instanceof HTMLElement) {
|
|
50
|
+
this._el.dataset[key] = value;
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
attributes(attr: Record<string, string | undefined>): this {
|
|
56
|
+
Object.entries(attr)
|
|
57
|
+
.filter(([_, value]) => value !== undefined)
|
|
58
|
+
.map(([key, value]) => this._el.setAttribute(key, value!));
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
style(styles: Partial<CSSStyleDeclaration>): this {
|
|
63
|
+
Object.assign(this._el.style, styles);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
children<C extends HTMLElement | SVGElement>(...children: Domino<C>[]): this {
|
|
68
|
+
children.forEach((child) => this._el.appendChild(child.root));
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
on(event: string, handler: (e: Event) => void): this {
|
|
73
|
+
this._el.addEventListener(event, handler);
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get root(): T {
|
|
78
|
+
return this._el;
|
|
79
|
+
}
|
|
80
|
+
}
|