Skip to content

Commit

Permalink
ECHOES-315 Add ToggleTip component
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-davis-sonarsource committed Jan 2, 2025
1 parent cda08f6 commit 62ea160
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 52 deletions.
12 changes: 11 additions & 1 deletion design-tokens/tokens/layer3/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
"type": "sizing"
}
},
"toggletips": {
"height": {
"value": "1rem",
"type": "sizing"
},
"width": {
"value": "1rem",
"type": "sizing"
}
},
"typography": {
"maxWidth": {
"default": {
Expand Down Expand Up @@ -102,4 +112,4 @@
}
}
}
}
}
4 changes: 4 additions & 0 deletions i18n/keys.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@
"select.clear": {
"defaultMessage": "Clear select field",
"description": "Screen reader-only text to indicate that the select field can be cleared with a button"
},
"toggletip.help": {
"defaultMessage": "More information",
"description": "aria-label text and tooltip for the ToggleTip"
}
}
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export * from './popover';
export * from './radio-button-group';
export * from './select';
export * from './spinner';
export * from './toggle-tip';
export * from './tooltip';
export * from './typography';
68 changes: 68 additions & 0 deletions src/components/toggle-tip/ToggleTip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Echoes React
* Copyright (C) 2023-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import styled from '@emotion/styled';
import { ComponentProps, forwardRef } from 'react';
import { useIntl } from 'react-intl';
import { ButtonIcon, ButtonVariety } from '../buttons';
import { IconInfo } from '../icons';
import { Popover } from '../popover';

type PopoverProps = Omit<ComponentProps<typeof Popover>, 'children'>;

interface Props extends PopoverProps {
ariaLabel?: string;
className?: string;
}

export const ToggleTip = forwardRef<HTMLButtonElement, Props>((props: Props, ref) => {
const { ariaLabel, className, ...popoverProps } = props;

const intl = useIntl();

const defaultAriaLabel = intl.formatMessage({
id: 'toggletip.help',
defaultMessage: 'More information',
description: 'aria-label text and tooltip for the ToggleTip',
});

return (
<Popover {...popoverProps}>
<ToggleTipButtonIcon
Icon={IconInfo}
ariaLabel={ariaLabel ?? defaultAriaLabel}
className={className}
ref={ref}
variety={ButtonVariety.DefaultGhost}
/>
</Popover>
);
});

const ToggleTipButtonIcon = styled(ButtonIcon)`
line-height: var(--echoes-line-height-10);
font-size: var(--echoes-font-size-10);
border-radius: var(--echoes-border-radius-200);
--button-padding: var(--echoes-dimension-space-0);
--button-height: var(--echoes-sizes-toggletips-height);
--button-width: var(--echoes-sizes-toggletips-width);
`;

ToggleTip.displayName = 'ToggleTip';
56 changes: 56 additions & 0 deletions src/components/toggle-tip/__tests__/ToggleTip-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Echoes React
* Copyright (C) 2023-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { screen } from '@testing-library/react';
import { ComponentProps } from 'react';
import { render } from '~common/helpers/test-utils';
import { ToggleTip } from '..';

it('appears when trigger is clicked and disappears when "esc" is hit', async () => {
const { user } = setupToggleTip();

expect(screen.queryByText('Header')).not.toBeInTheDocument();

await user.click(screen.getByLabelText('More information'));

expect(screen.getByText('Header')).toBeInTheDocument();

await user.keyboard('[escape]');

expect(screen.queryByText('Header')).not.toBeInTheDocument();
});

it('displays all additional parts', async () => {
const { user } = setupToggleTip({
ariaLabel: 'Trigger',
description: 'description',
extraContent: 'extra content',
footer: 'footer',
});

await user.click(screen.getByLabelText('Trigger'));

expect(screen.getByText('description')).toBeInTheDocument();
expect(screen.getByText('extra content')).toBeInTheDocument();
expect(screen.getByText('footer')).toBeInTheDocument();
});

function setupToggleTip(props: Partial<ComponentProps<typeof ToggleTip>> = {}) {
return render(<ToggleTip title="Header" {...props} />);
}
21 changes: 21 additions & 0 deletions src/components/toggle-tip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Echoes React
* Copyright (C) 2023-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

export { ToggleTip } from './ToggleTip';
2 changes: 2 additions & 0 deletions src/generated/design-tokens-base.css
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@
--echoes-sizes-inputs-full: 100%;
--echoes-sizes-buttons-medium: 2rem;
--echoes-sizes-buttons-large: 2.25rem;
--echoes-sizes-toggletips-height: 1rem;
--echoes-sizes-toggletips-width: 1rem;
--echoes-sizes-typography-max-width-default: 650px; /* Use as max width in typography components */
--echoes-sizes-logo-height-small: 2rem;
--echoes-sizes-logo-height-medium: 2.25rem;
Expand Down
4 changes: 4 additions & 0 deletions src/generated/design-tokens-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@
"medium": "2rem",
"large": "2.25rem"
},
"toggletips": {
"height": "1rem",
"width": "1rem"
},
"typography": {
"maxWidth": {
"default": "650px"
Expand Down
17 changes: 0 additions & 17 deletions src/generated/design-tokens-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,6 @@ html[data-echoes-theme='dark'], [data-echoes-theme='dark'] {
--echoes-color-border-warning: rgb(250,220,121);
--echoes-color-border-info: rgb(143,202,234);
--echoes-color-focus-default: rgb(189,198,255);
--echoes-sizes-overlays-max-width-default: 25rem;
--echoes-sizes-overlays-min-width-default: 150px;
--echoes-sizes-overlays-width-default: 37.5rem;
--echoes-sizes-overlays-width-wide: 62.5rem;
--echoes-sizes-overlays-max-height-default: 80vh;
--echoes-sizes-inputs-small: 150px;
--echoes-sizes-inputs-medium: 250px;
--echoes-sizes-inputs-large: 350px;
--echoes-sizes-inputs-full: 100%;
--echoes-sizes-buttons-medium: 2rem;
--echoes-sizes-buttons-large: 2.25rem;
--echoes-sizes-typography-max-width-default: 650px; /* Use as max width in typography components */
--echoes-sizes-logo-height-small: 2rem;
--echoes-sizes-logo-height-medium: 2.25rem;
--echoes-sizes-logo-height-large: 2.5rem;
--echoes-offset-dropdown-submenu-panel-vertical: 0px;
--echoes-offset-dropdown-submenu-panel-horizontal: 8px;
--echoes-box-shadow-xsmall: 0 1px 4px 0 rgba(8,9,12,0.4);
--echoes-box-shadow-small: 0 1px 10px 0 rgba(8,9,12,0.4), 0 1px 3px 0 rgba(8,9,12,0.4);
--echoes-box-shadow-medium: inset 0 2px 15px -2px rgba(8,9,12,0.06), 0 4px 15px -2px rgba(8,9,12,0.5);
Expand Down
17 changes: 0 additions & 17 deletions src/generated/design-tokens-light.css
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,6 @@
--echoes-color-border-warning: rgb(250,220,121);
--echoes-color-border-info: rgb(143,202,234);
--echoes-color-focus-default: rgb(75,86,187);
--echoes-sizes-overlays-max-width-default: 25rem;
--echoes-sizes-overlays-min-width-default: 150px;
--echoes-sizes-overlays-width-default: 37.5rem;
--echoes-sizes-overlays-width-wide: 62.5rem;
--echoes-sizes-overlays-max-height-default: 80vh;
--echoes-sizes-inputs-small: 150px;
--echoes-sizes-inputs-medium: 250px;
--echoes-sizes-inputs-large: 350px;
--echoes-sizes-inputs-full: 100%;
--echoes-sizes-buttons-medium: 2rem;
--echoes-sizes-buttons-large: 2.25rem;
--echoes-sizes-typography-max-width-default: 650px; /* Use as max width in typography components */
--echoes-sizes-logo-height-small: 2rem;
--echoes-sizes-logo-height-medium: 2.25rem;
--echoes-sizes-logo-height-large: 2.5rem;
--echoes-offset-dropdown-submenu-panel-vertical: 0px;
--echoes-offset-dropdown-submenu-panel-horizontal: 8px;
--echoes-box-shadow-xsmall: 0 1px 02px 0 rgba(0,0,0,0.05);
--echoes-box-shadow-small: 0 1px 25px 0 rgba(29,33,47,0.05), 0 1px 3px 0 rgba(29,33,47,0.05);
--echoes-box-shadow-medium: 0 2px 15px -2px rgba(29,33,47,0.06), 0 4px 8px -2px rgba(29,33,47,0.1);
Expand Down
17 changes: 0 additions & 17 deletions src/generated/design-tokens-themed.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,6 @@
"echoes-color-border-warning": "rgb(250,220,121)",
"echoes-color-border-info": "rgb(143,202,234)",
"echoes-color-focus-default": "rgb(75,86,187)",
"echoes-sizes-overlays-max-width-default": "25rem",
"echoes-sizes-overlays-min-width-default": "150px",
"echoes-sizes-overlays-width-default": "37.5rem",
"echoes-sizes-overlays-width-wide": "62.5rem",
"echoes-sizes-overlays-max-height-default": "80vh",
"echoes-sizes-inputs-small": "150px",
"echoes-sizes-inputs-medium": "250px",
"echoes-sizes-inputs-large": "350px",
"echoes-sizes-inputs-full": "100%",
"echoes-sizes-buttons-medium": "2rem",
"echoes-sizes-buttons-large": "2.25rem",
"echoes-sizes-typography-max-width-default": "650px",
"echoes-sizes-logo-height-small": "2rem",
"echoes-sizes-logo-height-medium": "2.25rem",
"echoes-sizes-logo-height-large": "2.5rem",
"echoes-offset-dropdown-submenu-panel-vertical": "0px",
"echoes-offset-dropdown-submenu-panel-horizontal": "8px",
"echoes-box-shadow-xsmall": "0 1px 02px 0 rgba(0,0,0,0.05)",
"echoes-box-shadow-small": "0 1px 25px 0 rgba(29,33,47,0.05), 0 1px 3px 0 rgba(29,33,47,0.05)",
"echoes-box-shadow-medium": "0 2px 15px -2px rgba(29,33,47,0.06), 0 4px 8px -2px rgba(29,33,47,0.1)",
Expand Down
75 changes: 75 additions & 0 deletions stories/ToggleTip-stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Echoes React
* Copyright (C) 2023-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import type { Meta, StoryObj } from '@storybook/react';
import { LinkStandalone, Text, ToggleTip } from '../src/components';
import { basicWrapperDecorator } from './helpers/BasicWrapper';

const meta: Meta<typeof ToggleTip> = {
component: ToggleTip,
title: 'Echoes/ToggleTip',
parameters: {
controls: { exclude: ['footer', 'extraContent'] },
},
decorators: [basicWrapperDecorator],
};

export default meta;

type Story = StoryObj<typeof ToggleTip>;

export const Basic: Story = {
args: {
title: 'Explanation',
description:
'This is a more complete explanation to detail the concepts and practices that are required to understand said explanation. It is fairly easy to assess how this important text is made important by its self-sufficient importance.',
},
render: (args) => <ToggleTip {...args} />,
};

export const Advanced: Story = {
args: {
title: 'Explanation',
description:
'This is a more complete explanation to detail the concepts and practices that are required to understand said explanation. It is fairly easy to assess how this important text is made important by its self-sufficient importance.',
},
render: (args) => (
<ToggleTip
extraContent={
<Text>
<span>First, click on the thing, then the other thing.</span>
<ul>
<li>Field 1: put your name</li>
<li>Field 2: put your favorite food item</li>
</ul>
<span>Finally, profit!</span>
</Text>
}
footer={
<>
<LinkStandalone to="#">Go to the config page</LinkStandalone>
{' - '}
<LinkStandalone to="#">Go to the docs</LinkStandalone>
</>
}
{...args}
/>
),
};

0 comments on commit 62ea160

Please sign in to comment.