Skip to content

Commit

Permalink
CopyButton: i18n & remove prop activeTitle (#3426)
Browse files Browse the repository at this point in the history
Co-authored-by: Halvor Haugan <[email protected]>
  • Loading branch information
it-vegard and HalvorHaugan authored Dec 5, 2024
1 parent 4896bb1 commit 89e069d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-ladybugs-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": minor
---

CopyButton: Removed prop `activeTitle`. Use `activeText` instead.
23 changes: 10 additions & 13 deletions @navikt/core/react/src/copybutton/CopyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CheckmarkIcon, FilesIcon } from "@navikt/aksel-icons";
import { Label } from "../typography";
import { composeEventHandlers } from "../util/composeEventHandlers";
import copy from "../util/copy";
import { useI18n } from "../util/i18n/i18n.context";

export interface CopyButtonProps
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "children"> {
Expand All @@ -32,7 +33,7 @@ export interface CopyButtonProps
text?: string;
/**
* Text shown when button is clicked.
* Only set if used with `text`-prop.
* Will be used as accessible label (title) if `text`-prop is not set.
* @default "Kopiert!"
*/
activeText?: string;
Expand Down Expand Up @@ -62,11 +63,6 @@ export interface CopyButtonProps
* @default "Kopier"
*/
title?: string;
/**
* Accessible label for icon in active-state (ignored if text is set).
* @default "Kopiert"
*/
activeTitle?: string;
/**
* Icon position in button.
* @default "left"
Expand All @@ -91,15 +87,14 @@ export const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>(
className,
copyText,
text,
activeText = "Kopiert!",
activeText,
variant = "neutral",
size = "medium",
onActiveChange,
icon,
activeIcon,
activeDuration = 2000,
title = "Kopier",
activeTitle = "Kopiert",
title,
iconPosition = "left",
onClick,
...rest
Expand All @@ -108,6 +103,7 @@ export const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>(
) => {
const [active, setActive] = useState(false);
const timeoutRef = useRef<number>();
const translate = useI18n("CopyButton");

useEffect(() => {
return () => {
Expand All @@ -127,19 +123,21 @@ export const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>(
}, activeDuration);
};

const activeString = activeText || translate("activeText");

const copyIcon = (
<span className="navds-copybutton__icon">
{active
? activeIcon ?? (
<CheckmarkIcon
aria-hidden={!!text}
title={text ? undefined : activeTitle}
title={text ? undefined : activeString}
/>
)
: icon ?? (
<FilesIcon
aria-hidden={!!text}
title={text ? undefined : title}
title={text ? undefined : title || translate("title")}
/>
)}
</span>
Expand All @@ -150,7 +148,6 @@ export const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>(
ref={ref}
type="button"
{...rest}
aria-live="assertive"
className={cl(
"navds-copybutton",
className,
Expand All @@ -168,7 +165,7 @@ export const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>(
{iconPosition === "left" && copyIcon}
{text && (
<Label as="span" size={size === "medium" ? "medium" : "small"}>
{active ? activeText : text}
{active ? activeString : text}
</Label>
)}
{iconPosition === "right" && copyIcon}
Expand Down
5 changes: 5 additions & 0 deletions @navikt/core/react/src/copybutton/copy-button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ export const Default: Story = {
control: { type: "radio" },
options: ["neutral", "action"],
},
iconPosition: {
control: { type: "radio" },
options: ["left", "right"],
},
},
args: {
size: "medium",
activeDuration: 2000,
copyText: "3.14",
text: "",
activeText: "",
title: "",
},
};

Expand Down
4 changes: 4 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export default {
labelSuffix: "delete",
},
},
CopyButton: {
title: "Copy",
activeText: "Copied!",
},
ErrorSummary: {
heading: "You must correct the following errors before you can continue:",
},
Expand Down
4 changes: 4 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default {
labelSuffix: "slett",
},
},
CopyButton: {
title: "Kopier",
activeText: "Kopiert!",
},
ErrorSummary: {
heading: "Du må rette disse feilene før du kan fortsette:",
},
Expand Down
4 changes: 4 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export default {
labelSuffix: "slett",
},
},
CopyButton: {
title: "Kopier",
activeText: "Kopiert!",
},
ErrorSummary: {
heading: "Du må rette desse feila før du kan halde fram:",
},
Expand Down

0 comments on commit 89e069d

Please sign in to comment.