@nurvus/ui 0.1.0-alpha.17 → 0.1.0-alpha.18

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.
Files changed (40) hide show
  1. package/README.md +7 -0
  2. package/dist/icons.svg +1 -0
  3. package/dist/index.js +1 -1
  4. package/dist/nurvus.css +4 -1
  5. package/dist/respawn.js +1 -1
  6. package/dist/types/anchor.d.ts +10 -2
  7. package/dist/types/components/Anchor/Anchor.d.ts +8 -5
  8. package/dist/types/components/Kbd/Kbd.d.ts +16 -0
  9. package/dist/types/components/NavToggle/NavToggle.d.ts +27 -0
  10. package/dist/types/components/Tooltip/Tooltip.d.ts +27 -0
  11. package/dist/types/icons.d.ts +1 -0
  12. package/dist/types/index.d.ts +7 -0
  13. package/dist/types/respawn/Anchor.d.ts +2 -0
  14. package/dist/types/respawn/Kbd.d.ts +10 -0
  15. package/dist/types/respawn/NavToggle.d.ts +22 -0
  16. package/dist/types/respawn/Tooltip.d.ts +20 -0
  17. package/dist/types/respawn/index.d.ts +7 -0
  18. package/dist/types/tooltip.d.ts +4 -0
  19. package/package.json +1 -1
  20. package/src/anchor.ts +30 -7
  21. package/src/components/Anchor/Anchor.scss +26 -3
  22. package/src/components/Anchor/Anchor.tsx +23 -14
  23. package/src/components/Kbd/Kbd.md +14 -0
  24. package/src/components/Kbd/Kbd.scss +30 -0
  25. package/src/components/Kbd/Kbd.tsx +28 -0
  26. package/src/components/NavToggle/NavToggle.md +51 -0
  27. package/src/components/NavToggle/NavToggle.scss +162 -0
  28. package/src/components/NavToggle/NavToggle.tsx +68 -0
  29. package/src/components/Tooltip/Tooltip.md +19 -0
  30. package/src/components/Tooltip/Tooltip.scss +75 -0
  31. package/src/components/Tooltip/Tooltip.tsx +69 -0
  32. package/src/css.ts +3 -0
  33. package/src/icons.ts +1 -0
  34. package/src/index.ts +7 -0
  35. package/src/respawn/Anchor.ts +26 -17
  36. package/src/respawn/Kbd.ts +22 -0
  37. package/src/respawn/NavToggle.ts +78 -0
  38. package/src/respawn/Tooltip.ts +63 -0
  39. package/src/respawn/index.ts +7 -0
  40. package/src/tooltip.ts +14 -0
@@ -0,0 +1,63 @@
1
+ import classNames from 'classnames';
2
+ import CONSTANT from '../constants';
3
+ import { keyboardFocus } from '../tooltip';
4
+ import Kbd from './Kbd';
5
+ import { spawn, Child, ElementProps } from './spawn';
6
+ import { updatable, NurvusElement } from './update';
7
+ import '../components/Tooltip/Tooltip.scss';
8
+
9
+ export interface TooltipProps extends ElementProps {
10
+ /** What the tooltip says. */
11
+ content: Child;
12
+ /** A keyboard shortcut to show after it, as key caps: ['⌘', 'B']. */
13
+ shortcut?: string[];
14
+ /** The tooltip's id: the control is described by it (aria-describedby). */
15
+ id: string;
16
+ /** Below the control (default) or above it. */
17
+ placement?: 'bottom' | 'top';
18
+ /** Which edge it lines up with: centered (default), or the control's start or end edge. */
19
+ align?: 'center' | 'start' | 'end';
20
+ /** How long the pointer must rest on the control before it shows, in ms. */
21
+ delay?: number;
22
+ /** The one control it describes (an element: it gets aria-describedby). */
23
+ children: HTMLElement;
24
+ }
25
+
26
+ /** A short label for a control, the same markup and behavior as the React Tooltip; it opens and closes in place. */
27
+ export default function Tooltip(props: TooltipProps): NurvusElement<HTMLSpanElement, TooltipProps> {
28
+ return updatable(({ content, shortcut, id, placement = 'bottom', align = 'center', delay = 300, children, className }: TooltipProps) => {
29
+ let timer: ReturnType<typeof setTimeout> | undefined;
30
+ const esc = (event: KeyboardEvent) => { if (event.key === 'Escape') set(false); };
31
+ let el: HTMLSpanElement;
32
+ function set(open: boolean) {
33
+ clearTimeout(timer as any);
34
+ el.setAttribute('data-open', String(open));
35
+ if (open) document.addEventListener('keydown', esc); else document.removeEventListener('keydown', esc);
36
+ }
37
+ children.setAttribute('aria-describedby', id);
38
+ el = spawn<HTMLSpanElement>({
39
+ tag: 'span',
40
+ className: classNames(className, 'tooltip', CONSTANT.CLASS_PREFIX),
41
+ 'data-open': false,
42
+ 'data-placement': placement,
43
+ 'data-align': align,
44
+ events: {
45
+ mouseenter: () => { clearTimeout(timer as any); timer = setTimeout(() => set(true), delay); },
46
+ mouseleave: () => set(false),
47
+ focusin: (event: FocusEvent) => { if (keyboardFocus(event.target)) set(true); },
48
+ focusout: () => set(false),
49
+ },
50
+ children: [
51
+ children,
52
+ spawn({
53
+ tag: 'span',
54
+ className: 'tooltip-bubble',
55
+ role: 'tooltip',
56
+ id,
57
+ children: [spawn({ tag: 'span', className: 'tooltip-text', children: content }), shortcut && shortcut.length ? Kbd({ keys: shortcut }) : null],
58
+ }),
59
+ ],
60
+ });
61
+ return el;
62
+ }, props);
63
+ }
@@ -57,6 +57,13 @@ export { default as Anchor } from './Anchor';
57
57
  export type { AnchorProps, AnchorItem } from './Anchor';
58
58
  export { default as Dropdown } from './Dropdown';
59
59
  export type { DropdownProps } from './Dropdown';
60
+ export { default as Kbd } from './Kbd';
61
+ export type { KbdProps } from './Kbd';
62
+ export { default as Tooltip } from './Tooltip';
63
+ export type { TooltipProps } from './Tooltip';
64
+ export { default as NavToggle } from './NavToggle';
65
+ export type { NavToggleProps, NavToggleMorph } from './NavToggle';
66
+ export { modKey } from '../tooltip';
60
67
  export { default as Menu } from './Menu';
61
68
  export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './Menu';
62
69
  export { default as Select } from './Select';
package/src/tooltip.ts ADDED
@@ -0,0 +1,14 @@
1
+ // Tooltip rules shared by the React and the Respawn Tooltip (no React here).
2
+
3
+ /** True when focus on `el` came from the keyboard (the browser shows a focus ring). */
4
+ export const keyboardFocus = (el: EventTarget | null): boolean => {
5
+ try { return !!el && (el as Element).matches(':focus-visible'); } catch (e) { return true; }
6
+ };
7
+
8
+ /** The platform's modifier for shortcuts: ⌘ on Apple devices, Ctrl elsewhere. */
9
+ export const modKey = (): '⌘' | 'Ctrl' => {
10
+ if (typeof navigator === 'undefined') return 'Ctrl';
11
+ const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
12
+ const platform = (nav.userAgentData && nav.userAgentData.platform) || navigator.platform || navigator.userAgent;
13
+ return /mac|iphone|ipad|ipod/i.test(platform) ? '⌘' : 'Ctrl';
14
+ };