@cccsaurora/howler-ui 3.2.0-dev.1783.1 → 3.2.0-dev.1791.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.
@@ -1,93 +1,123 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { Editor, useMonaco } from '@monaco-editor/react';
3
3
  import { useTheme } from '@mui/material';
4
- import { useAppTheme, useAppThemeBuilder } from '@tui/core';
5
4
  import { memo, useCallback, useEffect, useMemo } from 'react';
5
+ const toMonacoColor = (color) => {
6
+ const trimmedColor = color.trim();
7
+ const hexMatch = trimmedColor.match(/^#?([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i);
8
+ if (hexMatch) {
9
+ let hex = hexMatch[1] ?? '';
10
+ if (hex.length === 3 || hex.length === 4) {
11
+ hex = hex
12
+ .split('')
13
+ .map(character => character.repeat(2))
14
+ .join('');
15
+ }
16
+ return `#${hex.toUpperCase()}`;
17
+ }
18
+ const rgbMatch = trimmedColor.match(/^rgba?\((.*)\)$/i);
19
+ if (!rgbMatch) {
20
+ return trimmedColor;
21
+ }
22
+ const [channelValues = '', slashAlpha] = (rgbMatch[1] ?? '').split('/');
23
+ const channels = channelValues
24
+ .split(/[\s,]+/)
25
+ .filter(Boolean)
26
+ .map(channel => channel.trim());
27
+ const alpha = slashAlpha?.trim() ?? (channels.length > 3 ? channels.pop() : undefined);
28
+ if (channels.length !== 3 || !channels.every(channel => /^-?(?:\d+\.?\d*|\.\d+)%?$/.test(channel))) {
29
+ return trimmedColor;
30
+ }
31
+ const hexChannels = channels
32
+ .map(channel => {
33
+ const amount = Number.parseFloat(channel);
34
+ const value = channel.endsWith('%') ? (amount * 255) / 100 : amount;
35
+ return Math.round(Math.min(255, Math.max(0, value)))
36
+ .toString(16)
37
+ .padStart(2, '0');
38
+ })
39
+ .join('');
40
+ if (!alpha) {
41
+ return `#${hexChannels.toUpperCase()}`;
42
+ }
43
+ const alphaValue = Number.parseFloat(alpha);
44
+ if (/^-?(?:\d+\.?\d*|\.\d+)%?$/.test(alpha)) {
45
+ const normalizedAlpha = alpha.endsWith('%') ? alphaValue / 100 : alphaValue;
46
+ const alphaHex = Math.round(Math.min(1, Math.max(0, normalizedAlpha)) * 255)
47
+ .toString(16)
48
+ .padStart(2, '0');
49
+ return `#${hexChannels}${alphaHex}`.toUpperCase();
50
+ }
51
+ return trimmedColor;
52
+ };
53
+ const toMonacoTokenColor = (color) => toMonacoColor(color).replace(/^#/, '');
6
54
  const ThemedEditor = ({ beforeMount, options = {}, id, ...otherProps }) => {
7
- const { current: currentTheme, optionsOverride } = useAppTheme();
8
- const themeBuilder = useAppThemeBuilder();
9
- const { lightTheme, darkTheme } = useMemo(() => themeBuilder(currentTheme, optionsOverride), [themeBuilder, currentTheme, optionsOverride]);
10
55
  const theme = useTheme();
11
56
  const monaco = useMonaco();
12
- const _beforeMount = useCallback((_monaco) => {
13
- let lightBackground = lightTheme.palette.background.paper;
14
- // monaco doesn't like colours in the form #fff, with only three digits.
15
- if (lightBackground.startsWith('#') && lightBackground.length < 7) {
16
- lightBackground = lightBackground.replace(/(\w)/g, '$1$1');
17
- }
18
- _monaco.editor.defineTheme('howler', {
19
- base: 'vs',
20
- inherit: true,
21
- rules: [
22
- {
23
- token: 'handlebars',
24
- foreground: lightTheme.palette.warning.dark.toUpperCase().replaceAll('#', '')
25
- },
26
- {
27
- token: 'operator',
28
- foreground: lightTheme.palette.warning.light.toUpperCase().replaceAll('#', '')
29
- },
30
- {
31
- token: 'string.invalid',
32
- foreground: lightTheme.palette.error.main.toUpperCase().replaceAll('#', '')
33
- },
34
- {
35
- token: 'invalid',
36
- foreground: lightTheme.palette.error.main.toUpperCase().replaceAll('#', '')
37
- },
38
- {
39
- token: 'boolean',
40
- foreground: lightTheme.palette.success.main.toUpperCase().replaceAll('#', '')
41
- }
42
- ],
43
- colors: {
44
- 'editor.background': lightBackground
57
+ const monacoTheme = useMemo(() => ({
58
+ base: theme.palette.mode === 'light' ? 'vs' : 'vs-dark',
59
+ inherit: true,
60
+ rules: [
61
+ {
62
+ token: 'handlebars',
63
+ foreground: toMonacoTokenColor(theme.palette.warning.dark)
64
+ },
65
+ {
66
+ token: 'operator',
67
+ foreground: toMonacoTokenColor(theme.palette.warning.light)
68
+ },
69
+ {
70
+ token: 'string.invalid',
71
+ foreground: toMonacoTokenColor(theme.palette.error.main)
72
+ },
73
+ {
74
+ token: 'invalid',
75
+ foreground: toMonacoTokenColor(theme.palette.error.main)
76
+ },
77
+ {
78
+ token: 'boolean',
79
+ foreground: toMonacoTokenColor(theme.palette.success.main)
45
80
  }
46
- });
47
- let darkBackground = darkTheme.palette.background.paper;
48
- // monaco doesn't like colours in the form #fff, with only three digits.
49
- if (darkBackground.startsWith('#') && darkBackground.length < 7) {
50
- darkBackground = darkBackground.replace(/(\w)/g, '$1$1');
81
+ ],
82
+ colors: {
83
+ 'editor.background': toMonacoColor(theme.palette.background.paper),
84
+ 'editor.foreground': toMonacoColor(theme.palette.text.primary),
85
+ 'editorGutter.background': toMonacoColor(theme.palette.background.paper),
86
+ 'editorLineNumber.foreground': toMonacoColor(theme.palette.text.disabled),
87
+ 'editorLineNumber.activeForeground': toMonacoColor(theme.palette.text.secondary),
88
+ 'editorCursor.foreground': toMonacoColor(theme.palette.primary.main),
89
+ 'editor.selectionBackground': toMonacoColor(theme.palette.action.selected),
90
+ 'editor.inactiveSelectionBackground': toMonacoColor(theme.palette.action.hover),
91
+ 'editor.lineHighlightBackground': toMonacoColor(theme.palette.action.hover),
92
+ 'editorWhitespace.foreground': toMonacoColor(theme.palette.divider),
93
+ 'editorIndentGuide.background': toMonacoColor(theme.palette.divider),
94
+ 'editorIndentGuide.activeBackground': toMonacoColor(theme.palette.text.disabled),
95
+ 'editorWidget.background': toMonacoColor(theme.palette.background.paper),
96
+ 'editorWidget.border': toMonacoColor(theme.palette.divider),
97
+ 'editorSuggestWidget.background': toMonacoColor(theme.palette.background.paper),
98
+ 'editorSuggestWidget.border': toMonacoColor(theme.palette.divider),
99
+ 'editorSuggestWidget.selectedBackground': toMonacoColor(theme.palette.action.selected),
100
+ 'editorHoverWidget.background': toMonacoColor(theme.palette.background.paper),
101
+ 'editorHoverWidget.border': toMonacoColor(theme.palette.divider),
102
+ 'editorOverviewRuler.border': toMonacoColor(theme.palette.divider),
103
+ 'scrollbarSlider.background': toMonacoColor(theme.palette.action.hover),
104
+ 'scrollbarSlider.hoverBackground': toMonacoColor(theme.palette.action.selected),
105
+ 'scrollbarSlider.activeBackground': toMonacoColor(theme.palette.action.active)
51
106
  }
52
- _monaco.editor.defineTheme('howler-dark', {
53
- base: 'vs-dark',
54
- inherit: true,
55
- rules: [
56
- {
57
- token: 'handlebars',
58
- foreground: darkTheme.palette.warning.dark.toUpperCase().replaceAll('#', '')
59
- },
60
- {
61
- token: 'operator',
62
- foreground: darkTheme.palette.warning.light.toUpperCase().replaceAll('#', '')
63
- },
64
- {
65
- token: 'string.invalid',
66
- foreground: darkTheme.palette.error.main.toUpperCase().replaceAll('#', '')
67
- },
68
- {
69
- token: 'invalid',
70
- foreground: darkTheme.palette.error.main.toUpperCase().replaceAll('#', '')
71
- },
72
- {
73
- token: 'boolean',
74
- foreground: darkTheme.palette.success.main.toUpperCase().replaceAll('#', '')
75
- }
76
- ],
77
- colors: {
78
- 'editor.background': darkBackground
79
- }
80
- });
107
+ }), [theme]);
108
+ const _beforeMount = useCallback((_monaco) => {
109
+ _monaco.editor.defineTheme('howler', monacoTheme);
81
110
  _monaco.languages.register({ id: 'lucene' });
82
111
  _monaco.languages.register({ id: 'eql' });
83
112
  beforeMount?.(_monaco);
84
- }, [beforeMount, lightTheme, darkTheme]);
113
+ }, [beforeMount, monacoTheme]);
85
114
  useEffect(() => {
86
115
  if (!monaco) {
87
116
  return;
88
117
  }
89
- monaco.editor.setTheme(theme.palette.mode === 'light' ? 'howler' : 'howler-dark');
90
- }, [monaco, theme.palette.background.paper, theme.palette.mode]);
118
+ monaco.editor.defineTheme('howler', monacoTheme);
119
+ monaco.editor.setTheme('howler');
120
+ }, [monaco, monacoTheme]);
91
121
  const _options = useMemo(() => ({
92
122
  automaticLayout: true,
93
123
  minimap: { enabled: false },
@@ -99,6 +129,6 @@ const ThemedEditor = ({ beforeMount, options = {}, id, ...otherProps }) => {
99
129
  },
100
130
  ...options
101
131
  }), [options]);
102
- return (_jsx(Editor, { ...otherProps, wrapperProps: { id }, theme: theme.palette.mode === 'light' ? 'howler' : 'howler-dark', beforeMount: _beforeMount, options: _options }));
132
+ return _jsx(Editor, { ...otherProps, wrapperProps: { id }, theme: "howler", beforeMount: _beforeMount, options: _options });
103
133
  };
104
134
  export default memo(ThemedEditor);
@@ -1,13 +1,28 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  /// <reference types="vitest" />
3
3
  import { render } from '@testing-library/react';
4
- import { describe, expect, it, vi } from 'vitest';
4
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
5
5
  const mockDefineTheme = vi.hoisted(() => vi.fn());
6
6
  const mockRegisterLanguage = vi.hoisted(() => vi.fn());
7
7
  const mockSetTheme = vi.hoisted(() => vi.fn());
8
8
  const capturedProps = vi.hoisted(() => ({ current: null }));
9
- let themeMode = 'light';
9
+ const createPalette = (mode = 'light', paper = mode === 'light' ? '#fff' : '#111', primary = mode === 'light' ? '#0055aa' : '#268bd2', foreground = mode === 'light' ? '#202020' : '#eee8d5', warningDark = '#abc') => ({
10
+ mode,
11
+ background: { paper },
12
+ primary: { main: primary },
13
+ text: { primary: foreground, secondary: '#777777', disabled: '#aaaaaa' },
14
+ divider: '#dddddd',
15
+ action: {
16
+ selected: 'rgba(10, 20, 30, 0.2)',
17
+ hover: 'rgba(10, 20, 30, 0.1)',
18
+ active: 'rgba(10, 20, 30, 0.3)'
19
+ },
20
+ warning: { dark: warningDark, light: '#def' },
21
+ error: { main: '#123456' },
22
+ success: { main: '#654321' }
23
+ });
10
24
  let monacoValue = null;
25
+ let themePalette = createPalette();
11
26
  vi.mock('@monaco-editor/react', () => ({
12
27
  Editor: (props) => {
13
28
  capturedProps.current = props;
@@ -16,51 +31,67 @@ vi.mock('@monaco-editor/react', () => ({
16
31
  useMonaco: () => monacoValue
17
32
  }));
18
33
  vi.mock('@mui/material', () => ({
19
- useTheme: () => ({ palette: { mode: themeMode, background: { paper: themeMode === 'light' ? '#fff' : '#111' } } })
20
- }));
21
- vi.mock('@tui/core', () => ({
22
- useAppTheme: () => ({ current: 'default', optionsOverride: { dense: true } }),
23
- useAppThemeBuilder: () => () => ({
24
- lightTheme: {
25
- palette: {
26
- background: { paper: '#fff' },
27
- warning: { dark: '#abc', light: '#def' },
28
- error: { main: '#123456' },
29
- success: { main: '#654321' }
30
- }
31
- },
32
- darkTheme: {
33
- palette: {
34
- background: { paper: '#111' },
35
- warning: { dark: '#aaa', light: '#bbb' },
36
- error: { main: '#222222' },
37
- success: { main: '#333333' }
38
- }
39
- }
40
- })
34
+ useTheme: () => ({ palette: themePalette })
41
35
  }));
42
36
  import ThemedEditor from './ThemedEditor';
43
37
  describe('ThemedEditor', () => {
38
+ beforeEach(() => {
39
+ themePalette = createPalette();
40
+ monacoValue = null;
41
+ vi.clearAllMocks();
42
+ });
44
43
  it('defines custom monaco themes and merges editor options', () => {
45
- monacoValue = { editor: { setTheme: mockSetTheme } };
44
+ monacoValue = { editor: { defineTheme: mockDefineTheme, setTheme: mockSetTheme } };
46
45
  render(_jsx(ThemedEditor, { id: "the-editor", options: { readOnly: true }, beforeMount: vi.fn(), value: "x" }));
47
46
  const fakeMonaco = {
48
47
  editor: { defineTheme: mockDefineTheme },
49
48
  languages: { register: mockRegisterLanguage }
50
49
  };
51
50
  capturedProps.current.beforeMount(fakeMonaco);
52
- expect(mockDefineTheme).toHaveBeenCalledWith('howler', expect.objectContaining({ colors: { 'editor.background': '#ffffff' } }));
53
- expect(mockDefineTheme).toHaveBeenCalledWith('howler-dark', expect.objectContaining({ colors: { 'editor.background': '#111111' } }));
51
+ expect(mockDefineTheme).toHaveBeenCalledWith('howler', expect.objectContaining({
52
+ base: 'vs',
53
+ colors: expect.objectContaining({ 'editor.background': '#FFFFFF', 'editor.foreground': '#202020' })
54
+ }));
54
55
  expect(mockRegisterLanguage).toHaveBeenCalledWith({ id: 'lucene' });
55
56
  expect(capturedProps.current.wrapperProps).toEqual({ id: 'the-editor' });
56
57
  expect(capturedProps.current.options).toMatchObject({ readOnly: true, automaticLayout: true });
57
58
  expect(mockSetTheme).toHaveBeenCalledWith('howler');
58
59
  });
59
- it('switches to the dark theme when the mui theme changes', () => {
60
- themeMode = 'dark';
61
- monacoValue = { editor: { setTheme: mockSetTheme } };
60
+ it('converts RGB palette colors to Monaco token colors', () => {
61
+ themePalette = createPalette('light', '#fff', '#0055aa', '#202020', 'RGB(126, 95, 0)');
62
+ monacoValue = { editor: { defineTheme: mockDefineTheme, setTheme: mockSetTheme } };
62
63
  render(_jsx(ThemedEditor, { value: "x" }));
63
- expect(capturedProps.current.theme).toBe('howler-dark');
64
- expect(mockSetTheme).toHaveBeenCalledWith('howler-dark');
64
+ capturedProps.current.beforeMount({
65
+ editor: { defineTheme: mockDefineTheme },
66
+ languages: { register: mockRegisterLanguage }
67
+ });
68
+ expect(mockDefineTheme).toHaveBeenCalledWith('howler', expect.objectContaining({
69
+ rules: expect.arrayContaining([expect.objectContaining({ token: 'handlebars', foreground: '7E5F00' })])
70
+ }));
71
+ });
72
+ it('uses and updates colors from the active app theme', () => {
73
+ themePalette = createPalette('dark', '#002B36');
74
+ monacoValue = { editor: { defineTheme: mockDefineTheme, setTheme: mockSetTheme } };
75
+ const { rerender } = render(_jsx(ThemedEditor, { value: "x" }));
76
+ expect(capturedProps.current.theme).toBe('howler');
77
+ expect(mockDefineTheme).toHaveBeenLastCalledWith('howler', expect.objectContaining({
78
+ base: 'vs-dark',
79
+ colors: expect.objectContaining({
80
+ 'editor.background': '#002B36',
81
+ 'editor.foreground': '#EEE8D5',
82
+ 'editor.selectionBackground': '#0A141E33'
83
+ })
84
+ }));
85
+ expect(mockSetTheme).toHaveBeenCalledWith('howler');
86
+ themePalette = createPalette('dark', '#101010', '#cc5500', '#f0f0f0');
87
+ rerender(_jsx(ThemedEditor, { value: "y" }));
88
+ expect(mockDefineTheme).toHaveBeenLastCalledWith('howler', expect.objectContaining({
89
+ base: 'vs-dark',
90
+ colors: expect.objectContaining({
91
+ 'editor.background': '#101010',
92
+ 'editor.foreground': '#F0F0F0',
93
+ 'editorCursor.foreground': '#CC5500'
94
+ })
95
+ }));
65
96
  });
66
97
  });
@@ -39,7 +39,7 @@ const PageCenterRoot = styled(PageCenter)(({ theme }) => ({
39
39
  wordBreak: 'break-word'
40
40
  },
41
41
  '.paragraph': {
42
- marginTop: '-96px',
42
+ marginTop: theme.spacing(-12),
43
43
  '& .spacer': {
44
44
  height: theme.spacing(14)
45
45
  },
@@ -1,10 +1,13 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  /// <reference types="vitest" />
3
+ import { createTheme, ThemeProvider } from '@mui/material';
3
4
  import { render, screen } from '@testing-library/react';
4
- import { describe, expect, it, vi } from 'vitest';
5
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
5
6
  const mockUseMediaQuery = vi.hoisted(() => vi.fn(() => false));
6
7
  const mockAppUser = vi.hoisted(() => ({ user: { is_admin: true } }));
7
8
  const mockLocation = vi.hoisted(() => ({ hash: '#overview' }));
9
+ const mockAppBar = vi.hoisted(() => ({ autoHide: false }));
10
+ const mockAppLayout = vi.hoisted(() => ({ current: 'side' }));
8
11
  vi.mock('@mui/material', async (importOriginal) => {
9
12
  const actual = await importOriginal();
10
13
  return {
@@ -13,9 +16,9 @@ vi.mock('@mui/material', async (importOriginal) => {
13
16
  };
14
17
  });
15
18
  vi.mock('@tui/core', () => ({
16
- PageCenter: ({ children }) => _jsx("div", { children: children }),
17
- useAppBar: () => ({ autoHide: false }),
18
- useAppLayout: () => ({ current: 'side' }),
19
+ PageCenter: ({ children, className }) => (_jsx("div", { className: className, children: children })),
20
+ useAppBar: () => mockAppBar,
21
+ useAppLayout: () => mockAppLayout,
19
22
  useAppUser: () => mockAppUser
20
23
  }));
21
24
  vi.mock('components/hooks/useScrollRestoration', () => ({
@@ -43,7 +46,12 @@ const config = {
43
46
  }
44
47
  }
45
48
  };
49
+ const documentationWithSpacing = (spacing) => (_jsx(ThemeProvider, { theme: createTheme({ spacing }), children: _jsx(ApiConfigContext.Provider, { value: { config }, children: _jsx(SearchDocumentation, {}) }) }));
46
50
  describe('SearchDocumentation', () => {
51
+ beforeEach(() => {
52
+ mockAppBar.autoHide = false;
53
+ mockAppLayout.current = 'side';
54
+ });
47
55
  it('returns nothing when indexes are unavailable', () => {
48
56
  const { container } = render(_jsx(ApiConfigContext.Provider, { value: { config: {} }, children: _jsx(SearchDocumentation, {}) }));
49
57
  expect(container).toBeEmptyDOMElement();
@@ -62,4 +70,39 @@ describe('SearchDocumentation', () => {
62
70
  expect(screen.getAllByText('ranges').length).toBeGreaterThan(0);
63
71
  expect(screen.getAllByText('reserved').length).toBeGreaterThan(0);
64
72
  });
73
+ it.each([
74
+ { density: 'default', spacing: 8 },
75
+ { density: 'compact', spacing: 6 },
76
+ { density: 'dense', spacing: 4 }
77
+ ])('keeps section spacing positive at $density density', ({ spacing }) => {
78
+ const { container } = render(documentationWithSpacing(spacing));
79
+ const paragraphs = container.querySelectorAll('.paragraph');
80
+ expect(paragraphs.length).toBeGreaterThan(0);
81
+ paragraphs.forEach(paragraph => {
82
+ const spacer = paragraph.querySelector('.spacer');
83
+ expect(paragraph).toHaveStyle({ marginTop: `${-12 * spacing}px` });
84
+ expect(spacer).toHaveStyle({ height: `${14 * spacing}px` });
85
+ expect(parseFloat(getComputedStyle(paragraph).marginTop) + parseFloat(getComputedStyle(spacer).height)).toBe(2 * spacing);
86
+ });
87
+ });
88
+ it('updates section offsets when density changes without remounting', () => {
89
+ const { container, rerender } = render(documentationWithSpacing(8));
90
+ const paragraph = container.querySelector('#overview');
91
+ expect(paragraph).toHaveStyle({ marginTop: '-96px' });
92
+ rerender(documentationWithSpacing(6));
93
+ expect(paragraph).toHaveStyle({ marginTop: '-72px' });
94
+ rerender(documentationWithSpacing(4));
95
+ expect(paragraph).toHaveStyle({ marginTop: '-48px' });
96
+ });
97
+ it.each([
98
+ { layout: 'side', marginTop: '0px' },
99
+ { layout: 'top', marginTop: '-48px' }
100
+ ])('preserves auto-hide offsets in the $layout layout at dense density', ({ layout, marginTop }) => {
101
+ mockAppBar.autoHide = true;
102
+ mockAppLayout.current = layout;
103
+ const { container } = render(documentationWithSpacing(4));
104
+ const paragraph = container.querySelector('#overview');
105
+ expect(paragraph).toHaveStyle({ marginTop });
106
+ expect(paragraph.classList.contains('autoHide')).toBe(layout === 'side');
107
+ });
65
108
  });
package/package.json CHANGED
@@ -97,7 +97,7 @@
97
97
  "url": "https://github.com/CybercentreCanada/howler"
98
98
  },
99
99
  "type": "module",
100
- "version": "3.2.0-dev.1783.1",
100
+ "version": "3.2.0-dev.1791.1",
101
101
  "exports": {
102
102
  "./i18n": "./i18n.js",
103
103
  "./index.css": "./index.css",