@atlaskit/editor-toolbar 0.2.2 → 0.3.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/index.js +15 -0
  3. package/dist/cjs/ui/ColorPalette/Color.compiled.css +25 -0
  4. package/dist/cjs/ui/ColorPalette/Color.js +76 -0
  5. package/dist/cjs/ui/ColorPalette/getColorMessage.js +20 -0
  6. package/dist/cjs/ui/ColorPalette/index.compiled.css +1 -0
  7. package/dist/cjs/ui/ColorPalette/index.js +118 -0
  8. package/dist/cjs/ui/ColorPalette/types.js +5 -0
  9. package/dist/cjs/ui/ColorPalette/utils.js +94 -0
  10. package/dist/cjs/ui/ToolbarDropdownMenu.js +43 -4
  11. package/dist/cjs/ui/ToolbarDropdownMenuContext.js +40 -0
  12. package/dist/cjs/ui/icons/AIChatIcon.js +24 -12
  13. package/dist/es2019/index.js +2 -0
  14. package/dist/es2019/ui/ColorPalette/Color.compiled.css +25 -0
  15. package/dist/es2019/ui/ColorPalette/Color.js +65 -0
  16. package/dist/es2019/ui/ColorPalette/getColorMessage.js +18 -0
  17. package/dist/es2019/ui/ColorPalette/index.compiled.css +1 -0
  18. package/dist/es2019/ui/ColorPalette/index.js +110 -0
  19. package/dist/es2019/ui/ColorPalette/types.js +1 -0
  20. package/dist/es2019/ui/ColorPalette/utils.js +83 -0
  21. package/dist/es2019/ui/ToolbarDropdownMenu.js +43 -4
  22. package/dist/es2019/ui/ToolbarDropdownMenuContext.js +26 -0
  23. package/dist/es2019/ui/icons/AIChatIcon.js +32 -10
  24. package/dist/esm/index.js +2 -0
  25. package/dist/esm/ui/ColorPalette/Color.compiled.css +25 -0
  26. package/dist/esm/ui/ColorPalette/Color.js +67 -0
  27. package/dist/esm/ui/ColorPalette/getColorMessage.js +14 -0
  28. package/dist/esm/ui/ColorPalette/index.compiled.css +1 -0
  29. package/dist/esm/ui/ColorPalette/index.js +109 -0
  30. package/dist/esm/ui/ColorPalette/types.js +1 -0
  31. package/dist/esm/ui/ColorPalette/utils.js +84 -0
  32. package/dist/esm/ui/ToolbarDropdownMenu.js +41 -4
  33. package/dist/esm/ui/ToolbarDropdownMenuContext.js +31 -0
  34. package/dist/esm/ui/icons/AIChatIcon.js +22 -11
  35. package/dist/types/index.d.ts +2 -0
  36. package/dist/types/ui/ColorPalette/Color.d.ts +11 -0
  37. package/dist/types/ui/ColorPalette/getColorMessage.d.ts +8 -0
  38. package/dist/types/ui/ColorPalette/index.d.ts +15 -0
  39. package/dist/types/ui/ColorPalette/types.d.ts +89 -0
  40. package/dist/types/ui/ColorPalette/utils.d.ts +40 -0
  41. package/dist/types/ui/ToolbarDropdownMenu.d.ts +3 -0
  42. package/dist/types/ui/ToolbarDropdownMenuContext.d.ts +12 -0
  43. package/dist/types/ui/icons/AIChatIcon.d.ts +7 -4
  44. package/dist/types-ts4.5/index.d.ts +2 -0
  45. package/dist/types-ts4.5/ui/ColorPalette/Color.d.ts +11 -0
  46. package/dist/types-ts4.5/ui/ColorPalette/getColorMessage.d.ts +8 -0
  47. package/dist/types-ts4.5/ui/ColorPalette/index.d.ts +15 -0
  48. package/dist/types-ts4.5/ui/ColorPalette/types.d.ts +89 -0
  49. package/dist/types-ts4.5/ui/ColorPalette/utils.d.ts +40 -0
  50. package/dist/types-ts4.5/ui/ToolbarDropdownMenu.d.ts +3 -0
  51. package/dist/types-ts4.5/ui/ToolbarDropdownMenuContext.d.ts +12 -0
  52. package/dist/types-ts4.5/ui/icons/AIChatIcon.d.ts +7 -4
  53. package/package.json +7 -5
  54. package/src/index.ts +3 -0
  55. package/src/ui/ColorPalette/Color.tsx +110 -0
  56. package/src/ui/ColorPalette/getColorMessage.ts +27 -0
  57. package/src/ui/ColorPalette/index.tsx +125 -0
  58. package/src/ui/ColorPalette/types.ts +96 -0
  59. package/src/ui/ColorPalette/utils.ts +102 -0
  60. package/src/ui/ToolbarDropdownMenu.tsx +51 -5
  61. package/src/ui/ToolbarDropdownMenuContext.tsx +44 -0
  62. package/src/ui/icons/AIChatIcon.tsx +34 -10
  63. package/tsconfig.app.json +0 -3
@@ -1,4 +1,5 @@
1
1
  import React, { type ReactNode } from 'react';
2
+ import { type OnOpenChangeArgs } from '@atlaskit/dropdown-menu';
2
3
  import { type ToolbarButtonGroupLocation } from '../types';
3
4
  type ToolbarDropdownMenuProps = {
4
5
  iconBefore: React.ReactNode;
@@ -7,6 +8,8 @@ type ToolbarDropdownMenuProps = {
7
8
  isDisabled?: boolean;
8
9
  testId?: string;
9
10
  label?: string;
11
+ isOpen?: boolean;
12
+ onOpenChange?: (args: OnOpenChangeArgs) => void;
10
13
  };
11
14
  export declare const ToolbarDropdownMenu: ({ iconBefore, groupLocation, children, isDisabled, testId, label, }: ToolbarDropdownMenuProps) => React.JSX.Element;
12
15
  export {};
@@ -0,0 +1,12 @@
1
+ import React from 'react';
2
+ interface ToolbarDropdownMenuContextValue {
3
+ openMenu: () => void;
4
+ closeMenu: () => void;
5
+ isOpen: boolean;
6
+ }
7
+ export declare const useToolbarDropdownMenu: () => ToolbarDropdownMenuContextValue;
8
+ interface ToolbarDropdownMenuProviderProps {
9
+ children: React.ReactNode;
10
+ }
11
+ export declare const ToolbarDropdownMenuProvider: ({ children, }: ToolbarDropdownMenuProviderProps) => React.JSX.Element;
12
+ export {};
@@ -1,7 +1,10 @@
1
- import React from 'react';
2
- type AIChatIconProps = {
1
+ /**
2
+ * @jsxRuntime classic
3
+ * @jsx jsx
4
+ */
5
+ interface AIChatIconProps {
3
6
  label: string;
4
7
  testId?: string;
5
- };
6
- export declare const AIChatIcon: ({ label, testId }: AIChatIconProps) => React.JSX.Element;
8
+ }
9
+ export declare const AIChatIcon: ({ label, testId }: AIChatIconProps) => JSX.Element;
7
10
  export {};
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "registry": "https://registry.npmjs.org/"
5
5
  },
6
- "version": "0.2.2",
6
+ "version": "0.3.0",
7
7
  "description": "Common UI for Toolbars across the platform",
8
8
  "atlassian": {
9
9
  "team": "Editor: Jenga",
@@ -28,18 +28,20 @@
28
28
  "@atlaskit/button": "^23.3.0",
29
29
  "@atlaskit/css": "^0.12.0",
30
30
  "@atlaskit/dropdown-menu": "^16.3.0",
31
- "@atlaskit/icon": "^27.11.0",
32
- "@atlaskit/icon-lab": "^5.4.0",
31
+ "@atlaskit/icon": "^27.12.0",
32
+ "@atlaskit/icon-lab": "^5.6.0",
33
33
  "@atlaskit/logo": "^19.6.0",
34
34
  "@atlaskit/popup": "^4.3.0",
35
35
  "@atlaskit/primitives": "^14.11.0",
36
36
  "@atlaskit/tokens": "^6.0.0",
37
37
  "@atlaskit/tooltip": "^20.4.0",
38
38
  "@babel/runtime": "^7.0.0",
39
- "@compiled/react": "^0.18.3"
39
+ "@compiled/react": "^0.18.3",
40
+ "chromatism": "^2.6.0"
40
41
  },
41
42
  "peerDependencies": {
42
- "react": "^18.2.0"
43
+ "react": "^18.2.0",
44
+ "react-intl-next": "npm:react-intl@^5.18.1"
43
45
  },
44
46
  "devDependencies": {
45
47
  "@af/integration-testing": "workspace:^",
package/src/index.ts CHANGED
@@ -11,6 +11,7 @@ export { ToolbarSection } from './ui/ToolbarSection';
11
11
  export { ToolbarTooltip } from './ui/ToolbarTooltip';
12
12
  export { ToolbarDropdownDivider } from './ui/ToolbarDropdownDivider';
13
13
  export { ToolbarColorSwatch } from './ui/ToolbarColorSwatch';
14
+ export { useToolbarDropdownMenu } from './ui/ToolbarDropdownMenuContext';
14
15
 
15
16
  export { AIAdjustLengthIcon } from './ui/icons/AIAdjustLengthIcon';
16
17
  export { AIChatIcon } from './ui/icons/AIChatIcon';
@@ -58,6 +59,8 @@ export { AlignTextRightIcon } from './ui/icons/AlignTextRightIcon';
58
59
  export { IndentIcon } from './ui/icons/IndentIcon';
59
60
  export { OutdentIcon } from './ui/icons/OutdentIcon';
60
61
 
62
+ export { default as ColorPalette } from './ui/ColorPalette';
63
+
61
64
  export type { IconComponent, ToolbarButtonGroupLocation } from './types';
62
65
 
63
66
  export { useToolbarUI, ToolbarUIProvider } from './hooks/ui-context';
@@ -0,0 +1,110 @@
1
+ /**
2
+ * @jsxRuntime classic
3
+ * @jsx jsx
4
+ */
5
+ import React, { useCallback, memo } from 'react';
6
+
7
+ import { css, jsx } from '@compiled/react';
8
+
9
+ import EditorDoneIcon from '@atlaskit/icon/core/migration/check-mark--editor-done';
10
+ import { token } from '@atlaskit/tokens';
11
+ import Tooltip from '@atlaskit/tooltip';
12
+
13
+ import type { ColorProps } from './types';
14
+
15
+ const buttonWrapperStyles = css({
16
+ borderColor: 'transparent',
17
+ borderStyle: 'solid',
18
+ borderWidth: '1px',
19
+ display: 'flex',
20
+ alignItems: 'center',
21
+ paddingTop: token('space.025', '2px'),
22
+ paddingRight: token('space.025', '2px'),
23
+ paddingBottom: token('space.025', '2px'),
24
+ paddingLeft: token('space.025', '2px'),
25
+ borderRadius: token('border.radius.100', '4px'),
26
+ '&:focus-within, &:focus, &:hover': {
27
+ borderColor: token('color.border'),
28
+ },
29
+ });
30
+
31
+ const buttonStyles = css({
32
+ height: token('space.300', '26px'),
33
+ width: token('space.300', '26px'),
34
+ backgroundColor: token('color.background.neutral'),
35
+ padding: 0,
36
+ borderRadius: token('border.radius.050', '4px'),
37
+ border: `1px solid ${token('color.border.inverse')}`,
38
+ cursor: 'pointer',
39
+ display: 'block',
40
+ position: 'relative',
41
+ '&:focus': {
42
+ outline: `2px solid ${token('color.border.focused')}`,
43
+ outlineOffset: token('space.025', '2px'),
44
+ },
45
+ });
46
+
47
+ /**
48
+ * Individual color palette item component
49
+ * Displays a single color swatch with tooltip and selection state
50
+ */
51
+ export const Color = memo<ColorProps>(({
52
+ autoFocus,
53
+ tabIndex,
54
+ value,
55
+ label,
56
+ isSelected,
57
+ borderColor,
58
+ checkMarkColor = token('color.icon.inverse', '#FFFFFF'),
59
+ hexToPaletteColor,
60
+ decorator,
61
+ onClick,
62
+ onKeyDown,
63
+ }) => {
64
+ const colorStyle = hexToPaletteColor ? hexToPaletteColor(value) : value;
65
+
66
+ const handleMouseDown = useCallback((e: React.MouseEvent) => {
67
+ e.preventDefault();
68
+ }, []);
69
+
70
+ const handleClick = useCallback((e: React.MouseEvent) => {
71
+ e.preventDefault();
72
+ onClick(value, label);
73
+ }, [onClick, value, label]);
74
+
75
+ const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
76
+ if (!onKeyDown) {
77
+ return;
78
+ }
79
+ e.preventDefault();
80
+ onKeyDown(value, label, e);
81
+ }, [onKeyDown, value, label]);
82
+
83
+ return (
84
+ <Tooltip content={label}>
85
+ <span css={buttonWrapperStyles}>
86
+ <button
87
+ type="button"
88
+ css={buttonStyles}
89
+ aria-label={label}
90
+ role="radio"
91
+ aria-checked={isSelected}
92
+ onClick={handleClick}
93
+ onKeyDown={handleKeyDown}
94
+ onMouseDown={handleMouseDown}
95
+ tabIndex={tabIndex}
96
+ style={{
97
+ backgroundColor: colorStyle || token('color.background.input', '#FFFFFF'),
98
+ border: `1px solid ${borderColor}`,
99
+ }}
100
+ autoFocus={autoFocus}
101
+ >
102
+ {!decorator && isSelected && (
103
+ <EditorDoneIcon LEGACY_primaryColor={checkMarkColor} label="" />
104
+ )}
105
+ {decorator}
106
+ </button>
107
+ </span>
108
+ </Tooltip>
109
+ );
110
+ });
@@ -0,0 +1,27 @@
1
+ import { type MessageDescriptor } from 'react-intl-next';
2
+
3
+ /**
4
+ * Retrieves the appropriate internationalization message for a given color
5
+ * @param messages - Record of color values to message descriptors
6
+ * @param color - The color value to look up
7
+ * @returns The message descriptor or undefined if not found
8
+ */
9
+ export default function getColorMessage(
10
+ messages: Record<string | number, MessageDescriptor>,
11
+ color: string,
12
+ ): MessageDescriptor | undefined {
13
+ const message = messages[color as keyof typeof messages];
14
+
15
+ if (!message) {
16
+ // eslint-disable-next-line no-console
17
+ console.warn(
18
+ `Color palette does not have an internationalization message for color ${color.toUpperCase()}.
19
+ You must add a message description to properly translate this color.
20
+ Using current label as default message.
21
+ This could have happened when someone changed the 'colorPalette' from 'adf-schema' without updating this file.
22
+ `,
23
+ );
24
+ }
25
+
26
+ return message;
27
+ }
@@ -0,0 +1,125 @@
1
+ import React, { useMemo } from 'react';
2
+
3
+ import chromatism from 'chromatism';
4
+ import { useIntl } from 'react-intl-next';
5
+
6
+ import { cssMap } from '@atlaskit/css';
7
+ import { Box } from '@atlaskit/primitives/compiled';
8
+ import { token, useThemeObserver } from '@atlaskit/tokens';
9
+
10
+ import { Color } from './Color';
11
+ import getColorMessage from './getColorMessage';
12
+ import type { ColorPaletteProps } from './types';
13
+ import {
14
+ DEFAULT_COLOR_PICKER_COLUMNS,
15
+ getColorsPerRowFromPalette,
16
+ getTokenCSSVariableValue,
17
+ } from './utils';
18
+
19
+ const styles = cssMap({
20
+ paletteWrapper: {
21
+ display: 'flex',
22
+ },
23
+ });
24
+
25
+ /**
26
+ * For a given color pick the color from a list of colors with
27
+ * the highest contrast
28
+ *
29
+ * @param color color string, supports HEX, RGB, RGBA etc.
30
+ * @param useIconToken boolean, describes if a token should be used for the icon color
31
+ * @return Highest contrast color in pool
32
+ */
33
+ function getCheckMarkColor(color: string, useIconToken: boolean): string {
34
+ const tokenVal = getTokenCSSVariableValue(color);
35
+ const colorValue = !!tokenVal ? tokenVal : color;
36
+
37
+ // eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage
38
+ const contrastColor = ['#FFFFFF', '#42526E'].sort(
39
+ (a, b) => chromatism.difference(b, colorValue) - chromatism.difference(a, colorValue),
40
+ )[0];
41
+
42
+ if (!useIconToken) {
43
+ return contrastColor;
44
+ }
45
+
46
+ // Use of these token comes from guidance from designers in the Design System team
47
+ // they are only intended for use with text colors (and there are different tokens
48
+ // planned to be used when this extended to be used with other palettes).
49
+ // eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage
50
+ return contrastColor === '#FFFFFF' ? token('color.icon.inverse') : token('color.icon');
51
+ }
52
+
53
+ /**
54
+ * ColorPalette component for displaying a grid of selectable colors
55
+ *
56
+ * Features:
57
+ * - Responsive grid layout
58
+ * - Keyboard navigation support
59
+ * - Accessibility compliance (ARIA attributes)
60
+ * - Theme-aware tooltips
61
+ * - Design token integration
62
+ * - Customizable color mapping
63
+ */
64
+ const ColorPalette = ({
65
+ cols = DEFAULT_COLOR_PICKER_COLUMNS,
66
+ onClick,
67
+ onKeyDown,
68
+ selectedColor,
69
+ paletteOptions,
70
+ }: ColorPaletteProps) => {
71
+ const { formatMessage } = useIntl();
72
+ const { palette, hexToPaletteColor, paletteColorTooltipMessages } = paletteOptions;
73
+
74
+ const { colorMode: tokenTheme } = useThemeObserver();
75
+ const useIconToken = !!hexToPaletteColor;
76
+
77
+ const colorsPerRow = useMemo(() => {
78
+ return getColorsPerRowFromPalette(palette, cols);
79
+ }, [palette, cols]);
80
+
81
+ return (
82
+ <>
83
+ {colorsPerRow.map((row) => (
84
+ <Box xcss={styles.paletteWrapper} key={`row-first-color-${row[0].value}`} role="radiogroup">
85
+ {row.map(({ value, label, border, message, decorator }) => {
86
+ let tooltipMessage = message;
87
+
88
+ // Override with theme-specific tooltip messages if provided
89
+ if (paletteColorTooltipMessages) {
90
+ if (tokenTheme === 'dark') {
91
+ tooltipMessage = getColorMessage(
92
+ paletteColorTooltipMessages.dark,
93
+ value.toUpperCase(),
94
+ );
95
+ }
96
+ if (tokenTheme === 'light') {
97
+ tooltipMessage = getColorMessage(
98
+ paletteColorTooltipMessages.light,
99
+ value.toUpperCase(),
100
+ );
101
+ }
102
+ }
103
+
104
+ return (
105
+ <Color
106
+ key={value}
107
+ value={value}
108
+ borderColor={border}
109
+ label={tooltipMessage ? formatMessage(tooltipMessage) : label}
110
+ onClick={onClick}
111
+ onKeyDown={onKeyDown}
112
+ isSelected={value === selectedColor}
113
+ checkMarkColor={getCheckMarkColor(value, useIconToken)}
114
+ hexToPaletteColor={hexToPaletteColor}
115
+ decorator={decorator}
116
+ />
117
+ );
118
+ })}
119
+ </Box>
120
+ ))}
121
+ </>
122
+ );
123
+ };
124
+
125
+ export default ColorPalette;
@@ -0,0 +1,96 @@
1
+ import { type ReactElement } from 'react';
2
+
3
+ import { type MessageDescriptor } from 'react-intl-next';
4
+
5
+ /**
6
+ * Represents a single color in the palette
7
+ */
8
+ export interface PaletteColor {
9
+ /** The color value (hex, token, etc.) */
10
+ value: string;
11
+ /** Display label for the color */
12
+ label: string;
13
+ /** Border color for the color swatch */
14
+ border: string;
15
+ /** Optional internationalization message */
16
+ message?: MessageDescriptor;
17
+ /** Optional decorator element to display instead of checkmark */
18
+ decorator?: ReactElement;
19
+ }
20
+
21
+ /**
22
+ * Array of palette colors
23
+ */
24
+ export type Palette = Array<PaletteColor>;
25
+
26
+ /**
27
+ * Tooltip messages for different themes
28
+ */
29
+ export type PaletteTooltipMessages = {
30
+ dark: Record<string, MessageDescriptor>;
31
+ light: Record<string, MessageDescriptor>;
32
+ };
33
+
34
+ /**
35
+ * Configuration options for the color palette
36
+ */
37
+ export interface PaletteOptions {
38
+ /** Array of colors to display */
39
+ palette: PaletteColor[];
40
+ /**
41
+ * Function to convert hex codes to design system tokens
42
+ * Different color palettes may use different mapping functions
43
+ */
44
+ hexToPaletteColor?: (hexColor: string) => string | undefined;
45
+ /**
46
+ * Tooltip messages for different color themes
47
+ * Consumer determines which tooltip messages to use
48
+ */
49
+ paletteColorTooltipMessages?: PaletteTooltipMessages;
50
+ }
51
+
52
+ /**
53
+ * Props for the main ColorPalette component
54
+ */
55
+ export interface ColorPaletteProps {
56
+ /** Currently selected color value */
57
+ selectedColor: string | null;
58
+ /** Callback when a color is clicked */
59
+ onClick: (value: string, label: string) => void;
60
+ /** Optional callback for keyboard navigation */
61
+ onKeyDown?: (value: string, label: string, event: React.KeyboardEvent) => void;
62
+ /** Number of columns in the palette grid */
63
+ cols?: number;
64
+ /** Optional CSS class name */
65
+ className?: string;
66
+ /** Palette configuration options */
67
+ paletteOptions: PaletteOptions;
68
+ }
69
+
70
+ /**
71
+ * Props for individual color palette items
72
+ */
73
+ export interface ColorProps {
74
+ /** The color value */
75
+ value: string;
76
+ /** Display label for accessibility */
77
+ label: string;
78
+ /** Tab index for keyboard navigation */
79
+ tabIndex?: number;
80
+ /** Whether this color is currently selected */
81
+ isSelected?: boolean;
82
+ /** Click handler */
83
+ onClick: (value: string, label: string) => void;
84
+ /** Keyboard event handler */
85
+ onKeyDown?: (value: string, label: string, event: React.KeyboardEvent) => void;
86
+ /** Border color for the swatch */
87
+ borderColor: string;
88
+ /** Color for the checkmark icon */
89
+ checkMarkColor?: string;
90
+ /** Whether to auto-focus this item */
91
+ autoFocus?: boolean;
92
+ /** Function to convert hex to palette color */
93
+ hexToPaletteColor?: (hexColor: string) => string | undefined;
94
+ /** Optional decorator element */
95
+ decorator?: ReactElement;
96
+ }
@@ -0,0 +1,102 @@
1
+ import type { PaletteColor } from './types';
2
+
3
+ /**
4
+ * Default number of columns in the color picker
5
+ */
6
+ export const DEFAULT_COLOR_PICKER_COLUMNS = 7;
7
+
8
+ /**
9
+ * Splits a palette array into rows based on the specified number of columns
10
+ * @param palette - Array of palette colors
11
+ * @param cols - Number of columns per row
12
+ * @returns Array of color rows
13
+ */
14
+ export function getColorsPerRowFromPalette(
15
+ palette: PaletteColor[],
16
+ cols: number = DEFAULT_COLOR_PICKER_COLUMNS,
17
+ ): PaletteColor[][] {
18
+ return palette.reduce((resultArray: PaletteColor[][], item: PaletteColor, index: number) => {
19
+ const chunkIndex = Math.floor(index / cols);
20
+
21
+ resultArray[chunkIndex] = resultArray[chunkIndex] || []; // start a new chunk
22
+ resultArray[chunkIndex].push(item);
23
+
24
+ return resultArray;
25
+ }, []);
26
+ }
27
+
28
+ /**
29
+ * Finds the row and column indices of a selected color in the palette grid
30
+ * @param colorsPerRow - 2D array of colors organized by rows
31
+ * @param selectedColor - The currently selected color value
32
+ * @returns Object containing row and column indices
33
+ */
34
+ export function getSelectedRowAndColumn(
35
+ colorsPerRow: PaletteColor[][],
36
+ selectedColor: string | null,
37
+ ) {
38
+ let selectedRowIndex = -1;
39
+ let selectedColumnIndex = -1;
40
+
41
+ colorsPerRow.forEach((row, rowIndex) => {
42
+ row.forEach(({ value }, columnIndex) => {
43
+ if (value === selectedColor) {
44
+ selectedRowIndex = rowIndex;
45
+ selectedColumnIndex = columnIndex;
46
+ }
47
+ });
48
+ });
49
+
50
+ return {
51
+ selectedRowIndex,
52
+ selectedColumnIndex,
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Finds the row and column indices of a selected color in a flat palette array
58
+ * @param palette - Flat array of palette colors
59
+ * @param selectedColor - The currently selected color value
60
+ * @param cols - Number of columns per row
61
+ * @returns Object containing row and column indices
62
+ */
63
+ export function getSelectedRowAndColumnFromPalette(
64
+ palette: PaletteColor[],
65
+ selectedColor: string | null,
66
+ cols: number = DEFAULT_COLOR_PICKER_COLUMNS,
67
+ ) {
68
+ const colorsPerRow = getColorsPerRowFromPalette(palette, cols);
69
+ return getSelectedRowAndColumn(colorsPerRow, selectedColor);
70
+ }
71
+
72
+ /**
73
+ * Extracts the actual color value from a CSS variable expression
74
+ * Handles both token variables and fallback values
75
+ * @param variableExpression - CSS variable expression (e.g., "var(--ds-background-accent-blue-subtle, #0052CC)")
76
+ * @returns The resolved color value or empty string if not found
77
+ */
78
+ export const getTokenCSSVariableValue = (variableExpression: string): string => {
79
+ // Match CSS variable pattern: var(--variable-name, fallback)
80
+ // Ignored via go/ees005
81
+ // eslint-disable-next-line require-unicode-regexp
82
+ const matcher = variableExpression.match(/var\(([^,\)]+)(,.*)?/);
83
+ if (matcher) {
84
+ const variable = matcher[1].trim();
85
+ const fallback = matcher[2] ? matcher[2].replace(',', '').trim() : '';
86
+
87
+ // Return fallback if we're in a server environment
88
+ if (typeof document === 'undefined') {
89
+ return fallback;
90
+ }
91
+
92
+ // Get the computed value from the document
93
+ const value = window
94
+ .getComputedStyle(document.documentElement)
95
+ .getPropertyValue(variable)
96
+ .trim();
97
+
98
+ return value || fallback;
99
+ }
100
+
101
+ return '';
102
+ };
@@ -1,11 +1,12 @@
1
- import React, { type ReactNode } from 'react';
1
+ import React, { type ReactNode, useCallback } from 'react';
2
2
 
3
- import DropdownMenu from '@atlaskit/dropdown-menu';
3
+ import DropdownMenu, { type OnOpenChangeArgs } from '@atlaskit/dropdown-menu';
4
4
 
5
5
  import { useToolbarUI } from '../hooks/ui-context';
6
6
  import { type ToolbarButtonGroupLocation } from '../types';
7
7
 
8
8
  import { ToolbarButton } from './ToolbarButton';
9
+ import { ToolbarDropdownMenuProvider, useToolbarDropdownMenu } from './ToolbarDropdownMenuContext';
9
10
 
10
11
  type ToolbarDropdownMenuProps = {
11
12
  iconBefore: React.ReactNode;
@@ -14,9 +15,11 @@ type ToolbarDropdownMenuProps = {
14
15
  isDisabled?: boolean;
15
16
  testId?: string;
16
17
  label?: string;
18
+ isOpen?: boolean;
19
+ onOpenChange?: (args: OnOpenChangeArgs) => void;
17
20
  };
18
21
 
19
- export const ToolbarDropdownMenu = ({
22
+ const ToolbarDropdownMenuContent = ({
20
23
  iconBefore,
21
24
  groupLocation,
22
25
  children,
@@ -25,6 +28,22 @@ export const ToolbarDropdownMenu = ({
25
28
  label,
26
29
  }: ToolbarDropdownMenuProps) => {
27
30
  const { onDropdownOpenChanged } = useToolbarUI();
31
+ const { closeMenu, isOpen, openMenu } = useToolbarDropdownMenu();
32
+
33
+ const handleOpenChange = useCallback((args: OnOpenChangeArgs) => {
34
+ onDropdownOpenChanged(args);
35
+ if (!args.isOpen) {
36
+ closeMenu();
37
+ }
38
+ }, [closeMenu, onDropdownOpenChanged]);
39
+
40
+ const handleClick = useCallback(() => {
41
+ if (!isOpen) {
42
+ openMenu();
43
+ } else {
44
+ closeMenu();
45
+ }
46
+ }, [closeMenu, openMenu, isOpen]);
28
47
 
29
48
  return (
30
49
  <DropdownMenu<HTMLButtonElement>
@@ -36,7 +55,10 @@ export const ToolbarDropdownMenu = ({
36
55
  aria-haspopup={triggerProps['aria-haspopup']}
37
56
  aria-controls={triggerProps['aria-controls']}
38
57
  onBlur={triggerProps.onBlur}
39
- onClick={triggerProps.onClick}
58
+ onClick={(e) => {
59
+ handleClick();
60
+ triggerProps.onClick && triggerProps.onClick(e);
61
+ }}
40
62
  onFocus={triggerProps.onFocus}
41
63
  testId={testId}
42
64
  iconBefore={iconBefore}
@@ -45,9 +67,33 @@ export const ToolbarDropdownMenu = ({
45
67
  label={label}
46
68
  />
47
69
  )}
48
- onOpenChange={onDropdownOpenChanged}
70
+ onOpenChange={handleOpenChange}
71
+ isOpen={isOpen}
49
72
  >
50
73
  {children}
51
74
  </DropdownMenu>
52
75
  );
53
76
  };
77
+
78
+ export const ToolbarDropdownMenu = ({
79
+ iconBefore,
80
+ groupLocation,
81
+ children,
82
+ isDisabled,
83
+ testId,
84
+ label,
85
+ }: ToolbarDropdownMenuProps) => {
86
+ return (
87
+ <ToolbarDropdownMenuProvider>
88
+ <ToolbarDropdownMenuContent
89
+ iconBefore={iconBefore}
90
+ groupLocation={groupLocation}
91
+ isDisabled={isDisabled}
92
+ testId={testId}
93
+ label={label}
94
+ >
95
+ {children}
96
+ </ToolbarDropdownMenuContent>
97
+ </ToolbarDropdownMenuProvider>
98
+ );
99
+ };