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

fix(HelpText): css alignments #2438

Merged
merged 16 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
6 changes: 6 additions & 0 deletions .changeset/tame-rats-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@digdir/designsystemet-css": patch
"@digdir/designsystemet-react": patch
---

HelpText: Render icon with pseudo element and require aria-label
eirikbacker marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion apps/theme/components/Previews/Components/Components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export const Components = () => {
<DropdownMenu.Item>Spansk</DropdownMenu.Item>
<DropdownMenu.Item>Fransk</DropdownMenu.Item>
</DropdownMenu.Content>
<HelpText title='Du har ikke valgt språk'>
<HelpText aria-label='Du har ikke valgt språk'>
Velg språk for å endre innholdet på siden
</HelpText>
</DropdownMenu.Root>
Expand Down
7 changes: 4 additions & 3 deletions packages/css/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
*/

@media (hover: hover) and (pointer: fine) {
/* Only use hover for non-touch devices to prevent sticky-hovering */
&:not(:disabled, [aria-disabled='true'], [aria-busy='true']):hover {
/* Only use hover for non-touch devices to prevent sticky-hovering, using :where to prevent adding specificity */
&:where(:not(:disabled, [aria-disabled='true'], [aria-busy='true'])):hover {
background: var(--dsc-button-background--hover);
}
}
Expand All @@ -156,7 +156,8 @@
opacity: var(--ds-disabled-opacity);
}

&:not(:disabled, [aria-disabled='true'], [aria-busy='true']):active {
/* Using :where to prevent adding specificity */
&:where(:not(:disabled, [aria-disabled='true'], [aria-busy='true'])):active {
background-color: var(--dsc-button-background--active);
}
}
90 changes: 41 additions & 49 deletions packages/css/helptext.css
Original file line number Diff line number Diff line change
@@ -1,60 +1,52 @@
.ds-helptext__button {
--dsc-helptext-color: var(--ds-color-neutral-text-default);
--dsc-helptext-icon-color: var(--ds-color-accent-base-default);
--dsc-helptext-icon-width: var(--ds-sizing-7);
--dsc-helptext-icon-height: var(--ds-sizing-7);

background-color: transparent;
border-radius: 50px;
padding: 0 !important;
cursor: pointer;
display: flex;
border: none;
min-width: 0;
min-height: 0;
}
.ds-helptext {
--dsc-helptext-icon-size: 70%;
--dsc-helptext-icon-url: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 8 14'%3E%3Cpath fill='%23000' fill-rule='evenodd' d='M4 11a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM4 0c2.2 0 4 1.66 4 3.7 0 .98-.42 1.9-1.17 2.6l-.6.54-.29.29c-.48.46-.87.93-.94 1.41V9.5H3v-.81c0-1.26.84-2.22 1.68-3L5.42 5C5.8 4.65 6 4.2 6 3.7 6 2.66 5.1 1.83 4 1.83c-1.06 0-1.92.75-2 1.7v.15H0C0 1.66 1.8 0 4 0Z' clip-rule='evenodd'/%3E%3C/svg%3E");
--dsc-helptext-size: var(--ds-sizing-7);

@media print {
.ds-helptext__button {
display: none;
@composes ds-focus from '../css/utilities.css';

border-radius: var(--ds-border-radius-full);
border: 2px solid;
height: var(--dsc-helptext-size);
min-height: 0;
min-width: 0;
padding: 0;
width: var(--dsc-helptext-size);

&::before,
&:has(+ :popover-open) {
background: currentcolor;
mask-composite: exclude;
mask-image: var(--dsc-helptext-icon-url), linear-gradient(currentcolor, currentcolor); /* Cut icon out of currentcolor surface */
mask-position: center;
mask-repeat: no-repeat;
mask-size:
var(--dsc-helptext-icon-size) var(--dsc-helptext-icon-size),
cover;
}
}

.ds-helptext__icon--filled {
display: none;
}
&::before {
content: '';
mask-image: var(--dsc-helptext-icon-url);
width: 100%;
height: 100%;
}

.ds-helptext__icon {
color: var(--dsc-helptext-icon-color);
width: var(--dsc-helptext-icon-width);
height: var(--dsc-helptext-icon-height);
}
&[data-size='sm'] {
--dsc-helptext-size: var(--ds-sizing-6);
}

.ds-helptext__button:where(:hover, :focus, [data-state^='open']) > .ds-helptext__icon {
display: none;
}
&[data-size='lg'] {
--dsc-helptext-size: var(--ds-sizing-8);
}

.ds-helptext__button:where(:hover, :focus, [data-state^='open']) > .ds-helptext__icon--filled {
display: inline-block;
@media print {
display: none;
}
}

.ds-helptext__content {
/* .ds-helptext__content {
color: var(--dsc-helptext-color);
width: fit-content;
max-width: 700px;
}

.ds-helptext--sm .ds-helptext__icon {
--dsc-helptext-icon-width: var(--ds-sizing-6);
--dsc-helptext-icon-height: var(--ds-sizing-6);
}

.ds-helptext--md .ds-helptext__icon {
--dsc-helptext-icon-width: var(--ds-sizing-7);
--dsc-helptext-icon-height: var(--ds-sizing-7);
}

.ds-helptext--lg .ds-helptext__icon {
--dsc-helptext-icon-width: var(--ds-sizing-8);
--dsc-helptext-icon-height: var(--ds-sizing-8);
}
} */
6 changes: 0 additions & 6 deletions packages/react/src/components/HelpText/HelpText.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@ import * as HelpTextStories from './HelpText.stories.tsx';

<Primary />
<Controls />

## Portal

Aktiver `portal` dersom innholdet blir klippet av andre elementer for at `Popover` skal vises øverst.

<Canvas of={HelpTextStories.Portal} />
11 changes: 1 addition & 10 deletions packages/react/src/components/HelpText/HelpText.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@ export default {

export const Preview: Story = {
args: {
title: 'Help text title',
'aria-label': 'Help text title',
children: 'Help text content',
size: 'md',
},
decorators,
};

export const Portal: Story = {
args: {
title: 'Help text title',
children: 'Help text content',
size: 'md',
placement: 'top',
},
};
2 changes: 1 addition & 1 deletion packages/react/src/components/HelpText/HelpText.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const render = (props: Partial<HelpTextProps> = {}) => {
...props,
};
renderRtl(
<HelpText title={'Helptext for test'} {...allProps}>
<HelpText aria-label={'Helptext for test'} {...allProps}>
Help
</HelpText>,
);
Expand Down
75 changes: 15 additions & 60 deletions packages/react/src/components/HelpText/HelpText.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import type { Placement } from '@floating-ui/utils';
import cl from 'clsx/lite';
import { forwardRef, useId, useState } from 'react';
import { forwardRef } from 'react';
import type { ButtonHTMLAttributes } from 'react';

import { Popover, type PopoverProps } from '../Popover';
import { Paragraph } from '../Typography/Paragraph';

import { HelpTextIcon } from './HelpTextIcon';

export type HelpTextProps = {
/**
* Title for screen readers.
* Required descriptive label for screen readers.
**/
title: string;
'aria-label': string;
/**
* Size of the helptext
* @default md
Expand All @@ -23,68 +20,26 @@ export type HelpTextProps = {
* @default 'right'
*/
placement?: Placement;
} & ButtonHTMLAttributes<HTMLButtonElement>;
} & Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'color'>;

export const HelpText = forwardRef<HTMLButtonElement, HelpTextProps>(
function HelpText(
{
title,
placement = 'right',
size = 'md',
className,
children,
...rest
}: HelpTextProps,
{ placement = 'right', size = 'md', className, children, ...rest },
ref,
) {
const [open, setOpen] = useState(false);
const randomId = useId();

return (
<>
<button
className={cl(
`ds-helptext--${size}`,
'ds-helptext__button',
`ds-focus`,
className,
)}
onClick={() => setOpen(!open)}
// @ts-ignore
popovertarget={randomId}
<Popover.Context>
<Popover.Trigger
className={cl('ds-helptext', className)}
ref={ref}
size={size}
variant='tertiary'
{...rest}
>
<HelpTextIcon
filled
className={cl(
`ds-helptext__icon`,
`ds-helptext__icon--filled`,
className,
)}
openState={open}
/>
<HelpTextIcon
className={cl(`ds-helptext__icon`, className)}
openState={open}
/>
<span className={`ds-sr-only`}>{title}</span>
</button>
{/* TODO: Why is popover wrapped in paragraph here? */}
<Paragraph size='md' asChild>
<Popover
id={randomId}
className='ds-helptext__content'
onClose={() => setOpen(false)}
open={open}
placement={placement}
size={size}
variant='info'
>
{children}
</Popover>
</Paragraph>
</>
/>
<Popover placement={placement} size={size} variant='info'>
{children}
</Popover>
</Popover.Context>
);
},
);
60 changes: 0 additions & 60 deletions packages/react/src/components/HelpText/HelpTextIcon.test.tsx

This file was deleted.

31 changes: 0 additions & 31 deletions packages/react/src/components/HelpText/HelpTextIcon.tsx

This file was deleted.

Loading