@neovici/cosmoz-input 3.0.0 → 3.2.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.
@@ -0,0 +1,6 @@
1
+ import { BaseInput } from './use-input';
2
+ import { Render, ObjectFromList } from './render';
3
+ declare const observedAttributes: string[];
4
+ declare type CosmozInput = HTMLElement & ObjectFromList<typeof observedAttributes> & BaseInput & Render;
5
+ export declare const Input: (host: CosmozInput) => import("lit-html").TemplateResult<1>;
6
+ export {};
@@ -0,0 +1,40 @@
1
+ import { html } from 'lit-html'; // eslint-disable-line object-curly-newline
2
+ import { live } from 'lit-html/directives/live.js';
3
+ import { ifDefined } from 'lit-html/directives/if-defined.js';
4
+ import { component } from 'haunted';
5
+ import { useInput, useAllowedPattern } from './use-input';
6
+ import { render, attributes } from './render';
7
+ const observedAttributes = [
8
+ 'type',
9
+ 'pattern',
10
+ 'allowed-pattern',
11
+ 'min',
12
+ 'max',
13
+ 'step',
14
+ ...attributes,
15
+ ];
16
+ export const Input = (host) => {
17
+ const { type = 'text', pattern, allowedPattern, autocomplete, value, placeholder, readonly, disabled, min, max, step, maxlength, } = host, { onChange, onFocus, onInput } = useInput(host), onBeforeInput = useAllowedPattern(allowedPattern);
18
+ return render(html `<input
19
+ id="input"
20
+ part="input"
21
+ type=${type}
22
+ pattern=${ifDefined(pattern)}
23
+ autocomplete=${ifDefined(autocomplete)}
24
+ placeholder=${placeholder || ' '}
25
+ ?readonly=${readonly}
26
+ ?aria-disabled=${disabled}
27
+ ?disabled=${disabled}
28
+ .value=${live(value ?? '')}
29
+ maxlength=${ifDefined(maxlength)}
30
+ @beforeinput=${onBeforeInput}
31
+ @input=${onInput}
32
+ @change=${onChange}
33
+ @focus=${onFocus}
34
+ @blur=${onFocus}
35
+ min=${ifDefined(min)}
36
+ max=${ifDefined(max)}
37
+ step=${ifDefined(step)}
38
+ />`, host);
39
+ };
40
+ customElements.define('cosmoz-input', component(Input, { observedAttributes }));
@@ -0,0 +1,6 @@
1
+ import { BaseInput } from './use-input';
2
+ import { Render, ObjectFromList } from './render';
3
+ declare const observedAttributes: string[];
4
+ declare type CosmozInput = HTMLElement & ObjectFromList<typeof observedAttributes> & BaseInput & Render;
5
+ export declare const Textarea: (host: CosmozInput) => import("lit-html").TemplateResult<1>;
6
+ export {};
@@ -0,0 +1,20 @@
1
+ import { html } from 'lit-html'; // eslint-disable-line object-curly-newline
2
+ import { live } from 'lit-html/directives/live.js';
3
+ import { ifDefined } from 'lit-html/directives/if-defined.js';
4
+ import { component } from 'haunted';
5
+ import { useInput, useAutosize } from './use-input';
6
+ import { render, attributes } from './render';
7
+ const observedAttributes = ['rows', ...attributes];
8
+ export const Textarea = (host) => {
9
+ const { autocomplete, value, placeholder, readonly, disabled, rows, cols, maxlength, } = host, { onChange, onFocus, onInput } = useInput(host);
10
+ useAutosize(host);
11
+ return render(html `
12
+ <textarea id="input" part="input" style="resize: none"
13
+ autocomplete=${ifDefined(autocomplete)}
14
+ placeholder=${placeholder || ' '}
15
+ rows=${rows ?? 1} cols=${ifDefined(cols)}
16
+ ?readonly=${readonly} ?aria-disabled=${disabled} ?disabled=${disabled}
17
+ .value=${live(value ?? '')} maxlength=${ifDefined(maxlength)} @input=${onInput}
18
+ @change=${onChange} @focus=${onFocus} @blur=${onFocus}>`, host);
19
+ };
20
+ customElements.define('cosmoz-textarea', component(Textarea, { observedAttributes }));
@@ -0,0 +1,3 @@
1
+ export * from './cosmoz-input';
2
+ export * from './cosmoz-textarea';
3
+ export * from './use-input';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './cosmoz-input';
2
+ export * from './cosmoz-textarea';
3
+ export * from './use-input';
@@ -0,0 +1,9 @@
1
+ export declare type ObjectFromList<T extends ReadonlyArray<string>, V = string> = {
2
+ [K in (T extends ReadonlyArray<infer U> ? U : never)]: V;
3
+ };
4
+ export interface Render {
5
+ label?: string;
6
+ invalid?: boolean;
7
+ errorMessage?: string;
8
+ }
9
+ export declare const render: <T>(control: T, { label, invalid, errorMessage }: Render) => import("lit-html").TemplateResult<1>, attributes: string[];
package/dist/render.js ADDED
@@ -0,0 +1,27 @@
1
+ import { html } from 'lit-html';
2
+ import { when } from 'lit-html/directives/when.js';
3
+ import { styles } from './styles';
4
+ export const render = (control, { label, invalid, errorMessage }) => html `
5
+ <style>
6
+ ${styles}
7
+ </style>
8
+ <div class="float" part="float">&nbsp;</div>
9
+ <div class="wrap" part="wrap">
10
+ <slot name="prefix"></slot>
11
+ <div class="control" part="control">
12
+ ${control}
13
+ ${when(label, () => html `<label for="input" part="label">${label}</label>`)}
14
+ </div>
15
+ <slot name="suffix"></slot>
16
+ </div>
17
+ <div class="line" part="line"></div>
18
+ ${when(invalid && errorMessage, () => html `<div class="error" part="error">${errorMessage}</div>`)}
19
+ `, attributes = [
20
+ 'autocomplete',
21
+ 'readonly',
22
+ 'disabled',
23
+ 'maxlength',
24
+ 'invalid',
25
+ 'no-label-float',
26
+ 'always-float-label',
27
+ ];
@@ -0,0 +1 @@
1
+ export declare const styles: string;
package/dist/styles.js ADDED
@@ -0,0 +1,160 @@
1
+ import { tagged as css } from '@neovici/cosmoz-utils';
2
+ export const styles = css `
3
+ :host {
4
+ --font-family: var(
5
+ --cosmoz-input-font-family,
6
+ var(--paper-font-subhead_-_font-family, 'Roboto', 'Noto', sans-serif)
7
+ );
8
+ --font-size: var(
9
+ --cosmoz-input-font-size,
10
+ var(--paper-font-subhead_-_font-size, 16px)
11
+ );
12
+ --line-height: var(
13
+ --cosmoz-input-line-height,
14
+ var(--paper-font-subhead_-_line-height, 24px)
15
+ );
16
+ --label-scale: var(--cosmoz-input-label-scale, 0.75);
17
+ --disabled-opacity: var(
18
+ --cosmoz-input-disabled-opacity,
19
+ var(--paper-input-container-disabled_-_opacity, 0.33)
20
+ );
21
+ --disabled-line-opacity: var(
22
+ --cosmoz-input-disabled-line-opacity,
23
+ var(--paper-input-container-underline-disabled_-_opacity, 1)
24
+ );
25
+ --invalid-color: var(
26
+ --cosmoz-input-invalid-color,
27
+ var(--paper-input-container-invalid-color, var(--error-color, #fc5c5b))
28
+ );
29
+ --bg: var(--cosmoz-input-background);
30
+ --focused-bg: var(--cosmoz-input-focused-background, var(--bg));
31
+ --color: var(--cosmoz-input-color, var(--secondary-text-color, #737373));
32
+ --focused-color: var(
33
+ --cosmoz-input-focused-color,
34
+ var(--primary-color, #3f51b5)
35
+ );
36
+
37
+ display: block;
38
+ padding: var(--cosmoz-input-padding, 8px 0);
39
+ padding-top: var(--paper-input-container_-_padding-top, 8px);
40
+ padding-bottom: var(--paper-input-container_-_padding-bottom, 8px);
41
+ position: relative;
42
+
43
+ font-family: var(--font-family);
44
+ font-size: var(--font-size);
45
+ line-height: var(--line-height);
46
+ }
47
+
48
+ :host([disabled]) {
49
+ opacity: var(--disabled-opacity);
50
+ pointer-events: none;
51
+ }
52
+
53
+ .float {
54
+ line-height: calc(var(--line-height) * var(--label-scale));
55
+ }
56
+
57
+ .wrap {
58
+ display: flex;
59
+ align-items: center;
60
+ position: relative;
61
+ }
62
+
63
+ .control {
64
+ flex: 1;
65
+ position: relative;
66
+ }
67
+
68
+ #input {
69
+ padding: 0;
70
+ margin: 0;
71
+ outline: none;
72
+ border: none;
73
+ width: 100%;
74
+ max-width: 100%;
75
+ display: block;
76
+ background: var(--bg);
77
+ line-height: inherit;
78
+ font-size: inherit;
79
+ }
80
+
81
+ :host(:focus-within) #input {
82
+ background: var(--focused-bg);
83
+ }
84
+ label {
85
+ position: absolute;
86
+ top: 0;
87
+ left: 0;
88
+ width: 100%;
89
+ transition: transform 0.25s, width 0.25s;
90
+ transform-origin: left top;
91
+ color: var(--color);
92
+ white-space: nowrap;
93
+ overflow: hidden;
94
+ text-overflow: ellipsis;
95
+ }
96
+
97
+ :host([always-float-label]) label,
98
+ #input:not(:placeholder-shown) + label {
99
+ transform: translateY(calc(var(--label-scale) * -100%))
100
+ scale(var(--label-scale));
101
+ }
102
+ #input:not(:placeholder-shown):focus + label {
103
+ color: var(--focused-color);
104
+ }
105
+
106
+ .line {
107
+ padding-top: 1px;
108
+ border-bottom: 1px solid var(--color);
109
+ position: relative;
110
+ }
111
+ .line::before {
112
+ content: '';
113
+ position: absolute;
114
+ display: block;
115
+ border-bottom: 2px solid transparent;
116
+ border-bottom-color: inherit;
117
+ left: 0;
118
+ right: 0;
119
+ top: 0;
120
+ transform: scale3d(0, 1, 1);
121
+ transform-origin: center center;
122
+ z-index: 1;
123
+ }
124
+ :host(:focus-within) .line::before {
125
+ transform: none;
126
+ transition: 0.25s transform ease;
127
+ }
128
+ :host(:focus-within) .line {
129
+ border-bottom-color: var(--focused-color);
130
+ }
131
+ :host([disabled]) .line {
132
+ border-bottom-style: dashed;
133
+ opacity: var(--disabled-line-opacity);
134
+ }
135
+
136
+ :host([no-label-float]) .float,
137
+ :host([no-label-float]) #input:not(:placeholder-shown) + label {
138
+ display: none;
139
+ }
140
+
141
+ .error {
142
+ font-size: 12px;
143
+ line-height: 20px;
144
+ overflow: hidden;
145
+ text-overflow: clip;
146
+ position: absolute;
147
+ max-width: 100%;
148
+ }
149
+ :host([invalid]) label,
150
+ .error {
151
+ color: var(--invalid-color);
152
+ }
153
+ :host([invalid]) .line {
154
+ border-bottom-color: var(--invalid-color);
155
+ }
156
+
157
+ #input::-webkit-inner-spin-button {
158
+ z-index: 1;
159
+ }
160
+ `;
@@ -0,0 +1,11 @@
1
+ export interface BaseInput extends HTMLElement {
2
+ value?: string | number;
3
+ maxRows?: number;
4
+ focused?: boolean;
5
+ disabled?: boolean;
6
+ }
7
+ export declare const useInput: <T extends BaseInput>(host: T) => {
8
+ onChange: (e: Event) => boolean;
9
+ onFocus: (e: FocusEvent) => void;
10
+ onInput: (e: InputEvent) => void;
11
+ }, useAllowedPattern: (allowedPattern: string | RegExp) => (<T extends InputEvent>(e: T) => void) | undefined, autosize: (input: HTMLElement) => void, limit: (input: HTMLElement, maxRows?: number) => void, useAutosize: <T extends BaseInput>(host: T) => void;
@@ -0,0 +1,64 @@
1
+ import { useCallback, useEffect, useMemo } from 'haunted';
2
+ import { useImperativeApi } from '@neovici/cosmoz-utils/hooks/use-imperative-api';
3
+ import { notifyProperty } from '@neovici/cosmoz-utils/hooks/use-notify-property';
4
+ // TODO: use useRef instead of callback with querySelector
5
+ export const useInput = (host) => {
6
+ const root = host.shadowRoot, onChange = useCallback((e) => host.dispatchEvent(new Event(e.type, { bubbles: e.bubbles })), []), onInput = useCallback((e) => notifyProperty(host, 'value', e.target.value), []), onFocus = useCallback((e) => notifyProperty(host, 'focused', e.type === 'focus'), []), focus = useCallback(() => root.querySelector('#input')?.focus(), []), validate = useCallback(() => {
7
+ const valid = root.querySelector('#input')?.checkValidity();
8
+ host.toggleAttribute('invalid', !valid);
9
+ return valid;
10
+ }, []);
11
+ useImperativeApi({ focus, validate }, [focus, validate]);
12
+ useEffect(() => {
13
+ const onMouseDown = (e) => {
14
+ if (e.defaultPrevented ||
15
+ host.disabled ||
16
+ e.target.matches('input, textarea, label')) {
17
+ return;
18
+ }
19
+ e.preventDefault(); // don't blur
20
+ if (!host.matches(':focus-within')) {
21
+ // if input not focused
22
+ focus(); // focus input
23
+ }
24
+ };
25
+ root.addEventListener('mousedown', onMouseDown);
26
+ return () => root.removeEventListener('mousedown', onMouseDown);
27
+ }, [focus]);
28
+ return {
29
+ onChange,
30
+ onFocus,
31
+ onInput,
32
+ };
33
+ }, useAllowedPattern = (allowedPattern) => useMemo(() => {
34
+ if (allowedPattern == null) {
35
+ return;
36
+ }
37
+ const regexp = new RegExp(allowedPattern, 'u');
38
+ return (e) => {
39
+ if (!e.defaultPrevented && e.data && !regexp.test(e.data)) {
40
+ e.preventDefault();
41
+ }
42
+ };
43
+ }, [allowedPattern]), autosize = (input) => {
44
+ input.style.height = '';
45
+ input.style.height = `${input.scrollHeight}px`;
46
+ }, limit = (input, maxRows = 0) => {
47
+ if (maxRows > 0) {
48
+ const rows = input.getAttribute('rows') ?? '', height = input.style.height;
49
+ input.style.height = '';
50
+ input.setAttribute('rows', maxRows);
51
+ input.style.maxHeight = input.getBoundingClientRect().height + 'px';
52
+ input.style.height = height;
53
+ input.setAttribute('rows', rows);
54
+ }
55
+ }, useAutosize = (host) => {
56
+ const { value, maxRows } = host, input = useMemo(() => () => host.shadowRoot.querySelector('#input'), []);
57
+ useEffect(() => limit(input(), maxRows), [maxRows, input]);
58
+ useEffect(() => autosize(input()), [input, value]);
59
+ useEffect(() => {
60
+ const el = input(), observer = new ResizeObserver(() => requestAnimationFrame(() => autosize(el)));
61
+ observer.observe(el);
62
+ return () => observer.unobserve(el);
63
+ }, [input]);
64
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-input",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "A input web component",
5
5
  "keywords": [
6
6
  "lit-html",