@atlaskit/editor-toolbar 0.2.3 → 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.
- package/CHANGELOG.md +8 -0
- package/dist/cjs/index.js +15 -0
- package/dist/cjs/ui/ColorPalette/Color.compiled.css +25 -0
- package/dist/cjs/ui/ColorPalette/Color.js +76 -0
- package/dist/cjs/ui/ColorPalette/getColorMessage.js +20 -0
- package/dist/cjs/ui/ColorPalette/index.compiled.css +1 -0
- package/dist/cjs/ui/ColorPalette/index.js +118 -0
- package/dist/cjs/ui/ColorPalette/types.js +5 -0
- package/dist/cjs/ui/ColorPalette/utils.js +94 -0
- package/dist/cjs/ui/ToolbarDropdownMenu.js +43 -4
- package/dist/cjs/ui/ToolbarDropdownMenuContext.js +40 -0
- package/dist/es2019/index.js +2 -0
- package/dist/es2019/ui/ColorPalette/Color.compiled.css +25 -0
- package/dist/es2019/ui/ColorPalette/Color.js +65 -0
- package/dist/es2019/ui/ColorPalette/getColorMessage.js +18 -0
- package/dist/es2019/ui/ColorPalette/index.compiled.css +1 -0
- package/dist/es2019/ui/ColorPalette/index.js +110 -0
- package/dist/es2019/ui/ColorPalette/types.js +1 -0
- package/dist/es2019/ui/ColorPalette/utils.js +83 -0
- package/dist/es2019/ui/ToolbarDropdownMenu.js +43 -4
- package/dist/es2019/ui/ToolbarDropdownMenuContext.js +26 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/ui/ColorPalette/Color.compiled.css +25 -0
- package/dist/esm/ui/ColorPalette/Color.js +67 -0
- package/dist/esm/ui/ColorPalette/getColorMessage.js +14 -0
- package/dist/esm/ui/ColorPalette/index.compiled.css +1 -0
- package/dist/esm/ui/ColorPalette/index.js +109 -0
- package/dist/esm/ui/ColorPalette/types.js +1 -0
- package/dist/esm/ui/ColorPalette/utils.js +84 -0
- package/dist/esm/ui/ToolbarDropdownMenu.js +41 -4
- package/dist/esm/ui/ToolbarDropdownMenuContext.js +31 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/ui/ColorPalette/Color.d.ts +11 -0
- package/dist/types/ui/ColorPalette/getColorMessage.d.ts +8 -0
- package/dist/types/ui/ColorPalette/index.d.ts +15 -0
- package/dist/types/ui/ColorPalette/types.d.ts +89 -0
- package/dist/types/ui/ColorPalette/utils.d.ts +40 -0
- package/dist/types/ui/ToolbarDropdownMenu.d.ts +3 -0
- package/dist/types/ui/ToolbarDropdownMenuContext.d.ts +12 -0
- package/dist/types-ts4.5/index.d.ts +2 -0
- package/dist/types-ts4.5/ui/ColorPalette/Color.d.ts +11 -0
- package/dist/types-ts4.5/ui/ColorPalette/getColorMessage.d.ts +8 -0
- package/dist/types-ts4.5/ui/ColorPalette/index.d.ts +15 -0
- package/dist/types-ts4.5/ui/ColorPalette/types.d.ts +89 -0
- package/dist/types-ts4.5/ui/ColorPalette/utils.d.ts +40 -0
- package/dist/types-ts4.5/ui/ToolbarDropdownMenu.d.ts +3 -0
- package/dist/types-ts4.5/ui/ToolbarDropdownMenuContext.d.ts +12 -0
- package/package.json +5 -3
- package/src/index.ts +3 -0
- package/src/ui/ColorPalette/Color.tsx +110 -0
- package/src/ui/ColorPalette/getColorMessage.ts +27 -0
- package/src/ui/ColorPalette/index.tsx +125 -0
- package/src/ui/ColorPalette/types.ts +96 -0
- package/src/ui/ColorPalette/utils.ts +102 -0
- package/src/ui/ToolbarDropdownMenu.tsx +51 -5
- package/src/ui/ToolbarDropdownMenuContext.tsx +44 -0
|
@@ -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
|
-
|
|
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={
|
|
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={
|
|
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
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import React, { createContext, useContext, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
interface ToolbarDropdownMenuContextValue {
|
|
4
|
+
openMenu: () => void;
|
|
5
|
+
closeMenu: () => void;
|
|
6
|
+
isOpen: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ToolbarDropdownMenuContext = createContext<ToolbarDropdownMenuContextValue | undefined>(undefined);
|
|
10
|
+
|
|
11
|
+
export const useToolbarDropdownMenu = () => {
|
|
12
|
+
const context = useContext(ToolbarDropdownMenuContext);
|
|
13
|
+
if (!context) {
|
|
14
|
+
throw new Error('useToolbarDropdownMenu must be used within ToolbarDropdownMenuProvider');
|
|
15
|
+
}
|
|
16
|
+
return context;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
interface ToolbarDropdownMenuProviderProps {
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const ToolbarDropdownMenuProvider = ({
|
|
24
|
+
children,
|
|
25
|
+
}: ToolbarDropdownMenuProviderProps) => {
|
|
26
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
27
|
+
|
|
28
|
+
const openMenu = () => setIsOpen(true);
|
|
29
|
+
const closeMenu = () => {
|
|
30
|
+
setIsOpen(false);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const contextValue: ToolbarDropdownMenuContextValue = {
|
|
34
|
+
openMenu,
|
|
35
|
+
closeMenu,
|
|
36
|
+
isOpen,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<ToolbarDropdownMenuContext.Provider value={contextValue}>
|
|
41
|
+
{children}
|
|
42
|
+
</ToolbarDropdownMenuContext.Provider>
|
|
43
|
+
);
|
|
44
|
+
};
|