@1doc/1ds-vue 0.0.1 → 2.7.1
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/CHANGELOG.md +681 -0
- package/README.md +3 -2
- package/package.json +23 -7
- package/src/index.ts +165 -16
- package/src/utils/bandeiras.ts +22 -0
- package/src/utils/chart.ts +682 -0
- package/src/utils/density.ts +27 -0
- package/src/utils/overlay-stack.ts +26 -0
- package/src/utils/theme.ts +152 -0
- package/src/utils/toast.ts +89 -0
- package/dist/index.css +0 -1
- package/dist/index.mjs +0 -1905
- package/dist/index.umd.js +0 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pilha global de overlays de diálogo abertos (Modal, ConfirmDialog).
|
|
3
|
+
*
|
|
4
|
+
* Motivação (simulação de adoção, time SaaS RH): Modal e ConfirmDialog
|
|
5
|
+
* escutavam Esc no document de forma independente — um único Esc com um
|
|
6
|
+
* ConfirmDialog empilhado sobre um Modal fechava os dois. Com a pilha,
|
|
7
|
+
* apenas o overlay do TOPO reage ao Esc.
|
|
8
|
+
*
|
|
9
|
+
* Singleton por bundle (estado de módulo), compartilhado entre os
|
|
10
|
+
* componentes gerados React e Vue.
|
|
11
|
+
*/
|
|
12
|
+
const stack: string[] = [];
|
|
13
|
+
|
|
14
|
+
/** Registra um overlay aberto no topo da pilha (idempotente). */
|
|
15
|
+
export const pushOverlay = (id: string): void => {
|
|
16
|
+
if (!stack.includes(id)) stack.push(id);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Remove um overlay da pilha (ao fechar ou desmontar). */
|
|
20
|
+
export const removeOverlay = (id: string): void => {
|
|
21
|
+
const index = stack.indexOf(id);
|
|
22
|
+
if (index !== -1) stack.splice(index, 1);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** true se o overlay é o do topo — o único que deve reagir ao Esc. */
|
|
26
|
+
export const isTopOverlay = (id: string): boolean => stack.length > 0 && stack[stack.length - 1] === id
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tipos de temas oficiais do Design System
|
|
3
|
+
*/
|
|
4
|
+
export type Theme = "1doc" | "1ds-red" | "1ds-green" | "1ds-orange" | "1ds-yellow" | "1ds-purple" | "1doc-dark" | "1ds-red-dark" | "1ds-green-dark" | "1ds-orange-dark" | "1ds-yellow-dark" | "1ds-purple-dark" | "1doc-high-contrast";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Nome de tema aceito pelas APIs: um tema oficial OU um tema custom de
|
|
8
|
+
* marca própria registrado via registerTheme() (white-label).
|
|
9
|
+
* O truque `(string & {})` preserva o autocomplete dos oficiais.
|
|
10
|
+
*/
|
|
11
|
+
export type ThemeName = Theme | (string & {});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Chave do localStorage para armazenar o tema selecionado
|
|
15
|
+
*/
|
|
16
|
+
const THEME_STORAGE_KEY = "umds-theme";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Tema padrão do sistema
|
|
20
|
+
*/
|
|
21
|
+
const DEFAULT_THEME: Theme = "1doc";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Temas oficiais (fonte única para validação e getAvailableThemes)
|
|
25
|
+
*/
|
|
26
|
+
const OFFICIAL_THEMES: Theme[] = ["1doc", "1ds-red", "1ds-green", "1ds-orange", "1ds-yellow", "1ds-purple", "1doc-dark", "1ds-red-dark", "1ds-green-dark", "1ds-orange-dark", "1ds-yellow-dark", "1ds-purple-dark", "1doc-high-contrast"];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Temas custom registrados em runtime (white-label).
|
|
30
|
+
*/
|
|
31
|
+
const customThemes: string[] = [];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Registra um tema de marca própria (white-label) para que setTheme,
|
|
35
|
+
* getTheme, initTheme e onThemeChange o aceitem como válido.
|
|
36
|
+
*
|
|
37
|
+
* O CSS do tema é responsabilidade do consumidor: um arquivo com
|
|
38
|
+
* `[data-theme="<nome>"]` sobrescrevendo os tokens de tema
|
|
39
|
+
* (`--umds-theme-*`) ou os semânticos (`--umds-semantic-color-*`),
|
|
40
|
+
* carregado após o semantic.css.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* registerTheme("acme");
|
|
44
|
+
* registerTheme("acme-dark");
|
|
45
|
+
* setTheme("acme"); // agora válido e persistido
|
|
46
|
+
*/
|
|
47
|
+
export const registerTheme = (theme: string): void => {
|
|
48
|
+
if (!customThemes.includes(theme) && !OFFICIAL_THEMES.includes(theme as Theme)) {
|
|
49
|
+
customThemes.push(theme);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Define o tema ativo no documento
|
|
55
|
+
* @param theme - Nome do tema a ser aplicado (oficial ou registrado)
|
|
56
|
+
*/
|
|
57
|
+
export const setTheme = (theme: ThemeName): void => {
|
|
58
|
+
if (typeof document !== "undefined") {
|
|
59
|
+
document.documentElement.setAttribute("data-theme", theme);
|
|
60
|
+
try {
|
|
61
|
+
localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
// Silenciosamente falha se localStorage não estiver disponível
|
|
64
|
+
console.warn("Failed to save theme to localStorage:", e);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Obtém o tema atual, considerando localStorage e preferências do sistema.
|
|
71
|
+
* Temas custom precisam estar registrados (registerTheme) ANTES da leitura,
|
|
72
|
+
* senão o valor persistido é descartado em favor do padrão.
|
|
73
|
+
* @returns O tema atualmente selecionado
|
|
74
|
+
*/
|
|
75
|
+
export const getTheme = (): ThemeName => {
|
|
76
|
+
if (typeof window === "undefined") {
|
|
77
|
+
return DEFAULT_THEME;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 1. Tenta buscar do localStorage
|
|
81
|
+
try {
|
|
82
|
+
const stored = localStorage.getItem(THEME_STORAGE_KEY);
|
|
83
|
+
if (stored && isValidTheme(stored)) {
|
|
84
|
+
return stored;
|
|
85
|
+
}
|
|
86
|
+
} catch (e) {
|
|
87
|
+
console.warn("Failed to read theme from localStorage:", e);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 2. Tenta buscar do atributo data-theme do HTML
|
|
91
|
+
const currentTheme = document.documentElement.getAttribute("data-theme");
|
|
92
|
+
if (currentTheme && isValidTheme(currentTheme)) {
|
|
93
|
+
return currentTheme;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 3. Retorna o tema padrão
|
|
97
|
+
return DEFAULT_THEME;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Inicializa o tema ao carregar a página
|
|
102
|
+
* Deve ser chamado o mais cedo possível para evitar flash de conteúdo.
|
|
103
|
+
* White-label: chame registerTheme() antes de initTheme() para que o tema
|
|
104
|
+
* custom persistido no localStorage seja restaurado.
|
|
105
|
+
*/
|
|
106
|
+
export const initTheme = (): void => {
|
|
107
|
+
const theme = getTheme();
|
|
108
|
+
setTheme(theme);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Valida se uma string é um tema válido (oficial ou registrado)
|
|
113
|
+
* @param theme - String a ser validada
|
|
114
|
+
* @returns true se o tema é válido
|
|
115
|
+
*/
|
|
116
|
+
function isValidTheme(theme: string): boolean {
|
|
117
|
+
return OFFICIAL_THEMES.includes(theme as Theme) || customThemes.includes(theme);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Retorna todos os temas disponíveis (oficiais + registrados)
|
|
122
|
+
* @returns Array com todos os temas disponíveis
|
|
123
|
+
*/
|
|
124
|
+
export const getAvailableThemes = (): ThemeName[] => {
|
|
125
|
+
return [...OFFICIAL_THEMES, ...customThemes];
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Hook para detectar mudanças de tema (útil para frameworks reativos)
|
|
130
|
+
* @param callback - Função a ser executada quando o tema mudar
|
|
131
|
+
* @returns Função para remover o listener
|
|
132
|
+
*/
|
|
133
|
+
export const onThemeChange = (callback: (theme: ThemeName) => void): (() => void) => {
|
|
134
|
+
if (typeof window === "undefined") {
|
|
135
|
+
return () => {};
|
|
136
|
+
}
|
|
137
|
+
const observer = new MutationObserver(mutations => {
|
|
138
|
+
mutations.forEach(mutation => {
|
|
139
|
+
if (mutation.type === "attributes" && mutation.attributeName === "data-theme") {
|
|
140
|
+
const newTheme = document.documentElement.getAttribute("data-theme");
|
|
141
|
+
if (newTheme && isValidTheme(newTheme)) {
|
|
142
|
+
callback(newTheme);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
observer.observe(document.documentElement, {
|
|
148
|
+
attributes: true,
|
|
149
|
+
attributeFilter: ["data-theme"]
|
|
150
|
+
});
|
|
151
|
+
return () => observer.disconnect();
|
|
152
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gerenciador imperativo de toasts com fila (achado 4/5 times na simulação
|
|
3
|
+
* de adoção: o Toast declarativo obrigava cada app a criar um host próprio,
|
|
4
|
+
* e mensagens em sequência se atropelavam).
|
|
5
|
+
*
|
|
6
|
+
* Uso:
|
|
7
|
+
* 1. Monte UM `<ToastStack />` na raiz do app (define a posição da pilha).
|
|
8
|
+
* 2. Dispare de qualquer lugar: `toast({ message: "Salvo!", variant: "positive" })`.
|
|
9
|
+
*
|
|
10
|
+
* O componente declarativo `<Toast />` continua existindo como primitiva
|
|
11
|
+
* para casos controlados simples.
|
|
12
|
+
*
|
|
13
|
+
* Framework-agnóstico: estado de módulo + pub/sub (singleton por bundle),
|
|
14
|
+
* consumido pelos ToastStack gerados de React e Vue.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export type ToastVariant = "positive" | "negative" | "info";
|
|
18
|
+
export interface ToastOptions {
|
|
19
|
+
/** Mensagem principal do toast */
|
|
20
|
+
message: string;
|
|
21
|
+
/** Descrição adicional abaixo da mensagem */
|
|
22
|
+
description?: string;
|
|
23
|
+
/** Variante semântica @default 'info' */
|
|
24
|
+
variant?: ToastVariant;
|
|
25
|
+
/** Auto-dismiss em ms; 0 = não fecha sozinho @default 5000 */
|
|
26
|
+
duration?: number;
|
|
27
|
+
/** Exibe o botão de fechar @default true */
|
|
28
|
+
closable?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface ToastData {
|
|
31
|
+
id: number;
|
|
32
|
+
message: string;
|
|
33
|
+
description?: string;
|
|
34
|
+
variant: ToastVariant;
|
|
35
|
+
duration: number;
|
|
36
|
+
closable: boolean;
|
|
37
|
+
}
|
|
38
|
+
type ToastListener = (items: ToastData[]) => void;
|
|
39
|
+
let sequence = 0;
|
|
40
|
+
let items: ToastData[] = [];
|
|
41
|
+
const listeners: ToastListener[] = [];
|
|
42
|
+
const notify = (): void => {
|
|
43
|
+
const snapshot = [...items];
|
|
44
|
+
listeners.forEach(listener => listener(snapshot));
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Enfileira um toast e retorna o id (para dismiss programático). */
|
|
48
|
+
export const toast = (options: ToastOptions): number => {
|
|
49
|
+
sequence += 1;
|
|
50
|
+
const id = sequence;
|
|
51
|
+
items = [...items, {
|
|
52
|
+
variant: "info",
|
|
53
|
+
duration: 5000,
|
|
54
|
+
closable: true,
|
|
55
|
+
...options,
|
|
56
|
+
id
|
|
57
|
+
}];
|
|
58
|
+
notify();
|
|
59
|
+
return id;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** Remove um toast específico da pilha. */
|
|
63
|
+
export const dismissToast = (id: number): void => {
|
|
64
|
+
const next = items.filter(item => item.id !== id);
|
|
65
|
+
if (next.length !== items.length) {
|
|
66
|
+
items = next;
|
|
67
|
+
notify();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Limpa a pilha inteira. */
|
|
72
|
+
export const dismissAllToasts = (): void => {
|
|
73
|
+
if (items.length === 0) return;
|
|
74
|
+
items = [];
|
|
75
|
+
notify();
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Inscreve um listener (chamado imediatamente com o estado atual).
|
|
80
|
+
* Retorna a função de desinscrição. Usado pelo ToastStack.
|
|
81
|
+
*/
|
|
82
|
+
export const subscribeToasts = (listener: ToastListener): (() => void) => {
|
|
83
|
+
listeners.push(listener);
|
|
84
|
+
listener([...items]);
|
|
85
|
+
return () => {
|
|
86
|
+
const index = listeners.indexOf(listener);
|
|
87
|
+
if (index !== -1) listeners.splice(index, 1);
|
|
88
|
+
};
|
|
89
|
+
}
|