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 tag badge to palette picker #8208

Merged
merged 15 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
@@ -1,4 +1,6 @@
import React, { useState } from 'react';
import { css } from '@emotion/react';

import {
euiPaletteColorBlind,
euiPaletteForStatus,
Expand All @@ -10,32 +12,50 @@ import {
EuiSpacer,
EuiCode,
EuiColorPalettePicker,
EuiText,
EuiFlexGroup,
} from '../../../../src/components/';

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

const palettes = [
/** 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 (fixed)',
title: 'EUI color blind',
palette: euiPaletteColorBlind(),
append: appendTitles && <AppendedTitleText label="fixed" />,
type: 'fixed',
},
{
value: 'pallette_2',
title: 'EUI palette for status (gradient)',
title: 'EUI palette for status',
palette: euiPaletteForStatus(5),
append: appendTitles && <AppendedTitleText label="gradient" />,
type: 'gradient',
},
{
value: 'pallette_3',
title: 'EUI palette for temperature (fixed)',
title: 'EUI palette for temperature',
palette: euiPaletteForTemperature(5),
append: appendTitles && <AppendedTitleText label="fixed" />,
type: 'fixed',
},
{
value: 'pallette_4',
title: 'Grayscale (gradient with stops)',
title: 'Grayscale',
palette: [
{
stop: 100,
Expand All @@ -54,11 +74,12 @@ const palettes = [
color: 'black',
},
],
append: appendTitles && <AppendedTitleText label="gradient with stops" />,
type: 'gradient',
},
{
value: 'pallette_5',
title: 'Grayscale (fixed with stops)',
title: 'Grayscale',
palette: [
{
stop: 100,
Expand All @@ -77,6 +98,7 @@ const palettes = [
color: 'black',
},
],
append: appendTitles && <AppendedTitleText label="fixed with stops" />,
type: 'fixed',
},
{
Expand All @@ -88,23 +110,35 @@ const palettes = [

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

return (
<>
<EuiSwitch
label={
<span>
Display selected item as a <EuiCode>title</EuiCode>
</span>
}
checked={selectionDisplay}
onChange={() => setSelectionDisplay(!selectionDisplay)}
/>
<EuiFlexGroup>
<EuiSwitch
label={
<span>
Display selected item as a <EuiCode>title</EuiCode>
</span>
}
checked={selectionDisplay}
onChange={() => setSelectionDisplay(!selectionDisplay)}
/>
<EuiSwitch
label={
<span>
Display <EuiCode>append</EuiCode> element on title
</span>
}
checked={showAppendedTitles}
onChange={() => setShowAppendedTitles(!showAppendedTitles)}
/>
</EuiFlexGroup>
<EuiSpacer />
<DisplayToggles canPrepend={true} canAppend={true} canReadOnly={false}>
<EuiColorPalettePicker
palettes={palettes}
palettes={getPalettes(showAppendedTitles)}
onChange={setPallette}
valueOfSelected={pallette}
selectionDisplay={selectionDisplay ? 'title' : 'palette'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ export const ColorPickerExample = {
palettes for categorical data and <EuiCode>gradient</EuiCode>{' '}
palettes for continuous data.
</p>
<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>
</>
),
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 @@ -427,10 +427,18 @@ exports[`EuiColorPalettePicker more props are propagated to each option 1`] = `
class="euiColorPalettePicker__item"
>
<div
aria-hidden="true"
class="euiText euiColorPalettePicker__itemTitle emotion-euiText-xs"
class="euiFlexGroup emotion-euiFlexGroup-l-flexStart-stretch-row"
>
Palette 1
<div
class="euiFlexItem emotion-euiFlexItem-grow-1"
>
<div
aria-hidden="true"
class="euiText euiColorPalettePicker__itemTitle emotion-euiText-xs"
>
Palette 1
</div>
</div>
</div>
<div
class="euiSpacer euiSpacer--xs emotion-euiSpacer-xs"
Expand Down Expand Up @@ -487,10 +495,18 @@ exports[`EuiColorPalettePicker more props are propagated to each option 1`] = `
class="euiColorPalettePicker__item"
>
<div
aria-hidden="true"
class="euiText euiColorPalettePicker__itemTitle emotion-euiText-xs"
class="euiFlexGroup emotion-euiFlexGroup-l-flexStart-stretch-row"
>
Linear Gradient
<div
class="euiFlexItem emotion-euiFlexItem-grow-1"
>
<div
aria-hidden="true"
class="euiText euiColorPalettePicker__itemTitle emotion-euiText-xs"
>
Linear Gradient
</div>
</div>
</div>
<div
class="euiSpacer euiSpacer--xs emotion-euiSpacer-xs"
Expand Down Expand Up @@ -527,10 +543,18 @@ exports[`EuiColorPalettePicker more props are propagated to each option 1`] = `
class="euiColorPalettePicker__item"
>
<div
aria-hidden="true"
class="euiText euiColorPalettePicker__itemTitle emotion-euiText-xs"
class="euiFlexGroup emotion-euiFlexGroup-l-flexStart-stretch-row"
>
Linear Gradient with stops
<div
class="euiFlexItem emotion-euiFlexItem-grow-1"
>
<div
aria-hidden="true"
class="euiText euiColorPalettePicker__itemTitle emotion-euiText-xs"
>
Linear Gradient with stops
</div>
</div>
</div>
<div
class="euiSpacer euiSpacer--xs emotion-euiSpacer-xs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
* Side Public License, v 1.
*/

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

import { euiPaletteColorBlind } from '../../../services';
import { within } from '../../../../.storybook/test';
import { LOKI_SELECTORS } from '../../../../.storybook/loki';
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,7 +54,59 @@ export const Playground: Story = {
palette: euiPaletteColorBlind(),
type: 'fixed',
},
{
value: 'palette2',
title: 'Palette 2',
palette: euiPaletteForStatus(10),
type: 'gradient',
},
],
valueOfSelected: 'palette1',
},
};

export const AppendedTitles: Story = {
parameters: {
controls: { include: ['palettes', 'valueOfSelected'] },
loki: {
chromeSelector: LOKI_SELECTORS.portal,
},
},
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',
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('show popover on click', async () => {
await waitFor(async () => {
await fireEvent.click(canvas.getByRole('button'));
});
await canvas.waitForEuiPopoverVisible();
});
},
};
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 responsive={false}>
<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