Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add append option to palette picker options #8208

Merged
merged 15 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eui/changelogs/upcoming/8208.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Updated `EuiColorPalettePicker` - adds `append` to `EuiColorPalettePickerPaletteProps` to support appending custom content to the title
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import React, { useState } from 'react';
import { css } from '@emotion/react';
import {
euiPaletteColorBlind,
euiPaletteForStatus,
euiPaletteForTemperature,
} from '../../../../src/services';

import {
EuiSwitch,
EuiSpacer,
EuiCode,
EuiText,
EuiColorPalettePicker,
} from '../../../../src/components';

import { DisplayToggles } from '../form_controls/display_toggles';

/** Text wrapper to remove text-decoration on appended text */
const AppendedTitleText = ({ label }) => (
<EuiText color="subdued" size="xs">
<span
css={css`
display: inline-block;
`}
>
{label}
</span>
</EuiText>
);

const getPalettes = (appendTitles) => [
{
value: 'pallette_1',
title: 'EUI color blind',
palette: euiPaletteColorBlind(),
append: appendTitles && <AppendedTitleText label="fixed" />,
type: 'fixed',
},
{
value: 'pallette_2',
title: 'EUI palette for status',
palette: euiPaletteForStatus(5),
append: appendTitles && <AppendedTitleText label="gradient" />,
type: 'gradient',
},
{
value: 'pallette_3',
title: 'EUI palette for temperature',
palette: euiPaletteForTemperature(5),
append: appendTitles && <AppendedTitleText label="fixed" />,
type: 'fixed',
},
{
value: 'pallette_4',
title: 'Grayscale',
palette: [
{
stop: 100,
color: 'white',
},
{
stop: 250,
color: 'lightgray',
},
{
stop: 320,
color: 'gray',
},
{
stop: 470,
color: 'black',
},
],
append: appendTitles && <AppendedTitleText label="gradient with stops" />,
type: 'gradient',
},
{
value: 'pallette_5',
title: 'Grayscale',
palette: [
{
stop: 100,
color: 'white',
},
{
stop: 250,
color: 'lightgray',
},
{
stop: 320,
color: 'gray',
},
{
stop: 470,
color: 'black',
},
],
append: appendTitles && <AppendedTitleText label="fixed with stops" />,
type: 'fixed',
},
{
value: 'custom',
title: 'Plain text as a custom option',
type: 'text',
},
];

export default () => {
const [showAppendedTitles, setShowAppendedTitles] = useState(true);
const [pallette, setPallette] = useState('pallette_1');

return (
<>
<EuiSwitch
label={
<span>
Display <EuiCode>append</EuiCode> element on title
</span>
}
checked={showAppendedTitles}
onChange={() => setShowAppendedTitles(!showAppendedTitles)}
/>
<EuiSpacer />
<DisplayToggles canPrepend={true} canAppend={true} canReadOnly={false}>
<EuiColorPalettePicker
palettes={getPalettes(showAppendedTitles)}
onChange={setPallette}
valueOfSelected={pallette}
/>
</DisplayToggles>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ const colorPalettePickerSnippet = `<EuiColorPalettePicker
/>
`;

import ColorPalettePickerAppendedTitle from './color_palette_picker_appended_title';
const colorPalettePickerAppendedTitleSource = require('!!raw-loader!./color_palette_picker_appended_title');
const colorPalettePickerAppendedTitleSnippet = `<EuiColorPalettePicker
palettes={[
{
value: 'palette1',
title: 'EUI color blind',
palette: euiPaletteColorBlind(),
append: 'fixed',
type: 'fixed',
},
{
value: 'palette2',
title: 'EUI palette for status',
palette: euiPaletteForStatus(5),
append: <EuiText color="subdued" size="xs">gradient</EuiText>,
type: 'gradient',
},
]}
onChange={onPaletteChange}
valueOfSelected={palette}
/>
`;

import Alpha from './alpha';
const alphaSource = require('!!raw-loader!./alpha');
const alphaSnippet = `<EuiColorPicker
Expand Down Expand Up @@ -284,6 +308,36 @@ export const ColorPickerExample = {
snippet: colorPalettePickerSnippet,
demo: <ColorPalettePicker />,
},
{
title: 'Color palette picker - Appended titles',
text: (
<>
<EuiText>
<p>
Each of the <EuiCode>palettes</EuiCode>, excluding{' '}
<EuiCode>type='text'</EuiCode> palettes, can use the{' '}
<EuiCode>append</EuiCode> prop to append an element to the right
of the title.
</p>
</EuiText>
</>
),
source: [
{
type: GuideSectionTypes.JS,
code: colorPalettePickerAppendedTitleSource,
},
],
props: {
EuiColorPalettePicker,
EuiColorPalettePickerPaletteTextProps,
EuiColorPalettePickerPaletteFixedProps,
EuiColorPalettePickerPaletteGradientProps,
PaletteColorStop,
},
snippet: colorPalettePickerAppendedTitleSnippet,
demo: <ColorPalettePickerAppendedTitle />,
},
{
title: 'Color palette display',
text: (
Expand Down
2 changes: 1 addition & 1 deletion packages/eui/src/components/badge/badge.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const euiBadgeStyles = (euiThemeContext: UseEuiTheme) => {
font-weight: ${euiTheme.font.weight.medium};
white-space: nowrap;
text-decoration: none;
cursor: default;
cursor: inherit;
Copy link
Contributor Author

@nickofthyme nickofthyme Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this so that when the badge is placed inside a clickable component, such that clicking the badge will bubble up and trigger a click handler, the cursor should adopt to the parent component style by default. If the badge itself is clickable, this cursor style is overridden.

border: ${euiTheme.border.width.thin} solid transparent;
border-radius: ${mathWithUnits(
euiTheme.border.radius.medium,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
* Side Public License, v 1.
*/

import React from 'react';
import { css } from '@emotion/react';
import type { Meta, StoryObj } from '@storybook/react';

import { euiPaletteColorBlind } from '../../../services';
import { euiPaletteColorBlind, euiPaletteForStatus } from '../../../services';

import {
EuiColorPalettePicker,
EuiColorPalettePickerProps,
} from './color_palette_picker';
import { EuiText } from '../../text';

const meta: Meta<EuiColorPalettePickerProps<string>> = {
title: 'Forms/EuiColorPalettePicker/EuiColorPalettePicker',
Expand Down Expand Up @@ -48,6 +51,46 @@ export const Playground: Story = {
palette: euiPaletteColorBlind(),
type: 'fixed',
},
{
value: 'palette2',
title: 'Palette 2',
palette: euiPaletteForStatus(10),
type: 'gradient',
},
],
valueOfSelected: 'palette1',
},
};

export const CustomTitles: Story = {
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
parameters: {
controls: { include: ['palettes', 'valueOfSelected'] },
},
args: {
palettes: [
{
value: 'palette1',
title: 'Elastic',
append: (
<EuiText color="subdued" size="xs">
<span
css={css`
display: inline-block;
`}
>
Default
</span>
</EuiText>
),
palette: euiPaletteColorBlind(),
type: 'fixed',
},
{
value: 'pallette2',
title: 'Status',
palette: euiPaletteForStatus(10),
type: 'gradient',
},
],
valueOfSelected: 'palette1',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
* Side Public License, v 1.
*/

import React, { FunctionComponent, useCallback, useMemo } from 'react';
import React, {
FunctionComponent,
ReactNode,
useCallback,
useMemo,
} from 'react';

import { CommonProps } from '../../common';
import { EuiSpacer } from '../../spacer';
Expand All @@ -17,6 +22,7 @@ import {
} from '../../form/super_select'; // Note: needs to be pointed at this specific subdir for Storybook to inherit types correctly??

import { EuiColorPaletteDisplay } from '../color_palette_display';
import { EuiFlexGroup, EuiFlexItem } from '../../flex';

export interface PaletteColorStop {
stop: number;
Expand All @@ -32,6 +38,10 @@ export interface EuiColorPalettePickerPaletteTextProps extends CommonProps {
* The name of your palette
*/
title: string;
/**
* Node appended to right of title - disallowed for text-only options
*/
append?: never;
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
/**
* `text`: a text only option (a title is required).
*/
Expand All @@ -50,7 +60,11 @@ export interface EuiColorPalettePickerPaletteFixedProps extends CommonProps {
/**
* The name of your palette
*/
title?: string;
title: string;
/**
* Node appended to right of title
*/
append?: ReactNode;
/**
* `fixed`: individual color blocks
*/
Expand All @@ -69,7 +83,11 @@ export interface EuiColorPalettePickerPaletteGradientProps extends CommonProps {
/**
* The name of your palette
*/
title?: string;
title: string;
/**
* Node appended to right of title
*/
append?: ReactNode;
/**
* `gradient`: each color fades into the next
*/
Expand Down Expand Up @@ -136,7 +154,7 @@ export const EuiColorPalettePicker: FunctionComponent<
const paletteOptions = useMemo(
() =>
palettes.map((item: EuiColorPalettePickerPaletteProps) => {
const { type, value, title, palette, ...rest } = item;
const { type, value, title, append, palette, ...rest } = item;
const paletteForDisplay =
item.type !== 'text' ? getPalette(item) : null;

Expand All @@ -153,13 +171,18 @@ export const EuiColorPalettePicker: FunctionComponent<
// color_palette_display_gradient. Adding the aria-hidden attribute
// here to ensure screen readers don't speak the listbox options twice.
<>
<EuiText
aria-hidden="true"
className="euiColorPalettePicker__itemTitle"
size="xs"
>
{title}
</EuiText>
<EuiFlexGroup>
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
<EuiFlexItem>
<EuiText
aria-hidden="true"
className="euiColorPalettePicker__itemTitle"
size="xs"
>
{title}
</EuiText>
</EuiFlexItem>
{append && <EuiFlexItem grow={0}>{append}</EuiFlexItem>}
</EuiFlexGroup>
<EuiSpacer size="xs" />
</>
)}
Expand Down
Loading
Loading