Skip to content

Bugfix/CopyButton avoid layout shift for multi lined texts #3788

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

it-vegard
Copy link
Contributor

Description

On the new token page, we have a CopyButton for copying the tokens. In some cases, the text wraps over two lines.
However, when copying, the active text is single-line, causing the UI to "jump". This PR fixes that by adding a minHeight and minWidth after first render.

Component Checklist 📝

  • JSDoc
  • Examples
  • Documentation / Decision Records
  • Storybook
  • Style mappings (@navikt/core/css/config/_mappings.js)
  • Component tokens (@navikt/core/css/tokens.json)
  • CSS class deprecations (@navikt/aksel-stylelint/src/deprecations.ts)
  • Exports (@navikt/core/react/src/index.ts and @navikt/core/react/package.json)
  • New component? CSS import (@navikt/core/css/index.css)
  • Breaking change? Update migration guide. Consider codemod.
  • Changeset (Format: <Component>: <gitmoji?> <Text>. E.g. "Button: ✨ Add feature xyz.")

Copy link

changeset-bot bot commented May 14, 2025

🦋 Changeset detected

Latest commit: f885732

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented May 14, 2025

Storybook demo / Chromatic

📝 Changes to review: 1

8c32b33b7 | 91 components | 135 stories

Copy link
Collaborator

@KenAJoh KenAJoh left a comment

Choose a reason for hiding this comment

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

This change is

Comment on lines +116 to +124
useEffect(() => {
if (buttonRef?.current) {
setFixedSize({
height: buttonRef?.current.offsetHeight,
width: buttonRef?.current.offsetWidth,
});
}
}, []);

Copy link
Collaborator

Choose a reason for hiding this comment

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

This code only runs on mount now, so screens that resizes etc will have a "wrong" sized button. Should this sizing be updated onClick/when button is active and be reset when not active?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can try that. 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

This change is only for "legacy" system, and not new darkside since we just use Button in "new" CopyButton

Copy link
Collaborator

Choose a reason for hiding this comment

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

This change only handles when the active text is shorter/fewer lines than the default text. In some cases one can have a longer active text and with this update that will break

// works
<CopyButton activeText="short text" text="long text" />

// Does not work
<CopyButton activeText="long text" text="short text" />

@KenAJoh KenAJoh requested a review from Copilot May 14, 2025 11:14
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request fixes a layout shift issue in the CopyButton component when dealing with multi-lined texts by preserving the button's dimensions after its first render.

  • Introduces a fixed size measurement of the button using refs and state.
  • Applies new inline styles to both the Button and button elements to prevent UI jump.
  • Updates storybook and CSS to support the new layout behavior.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
@navikt/core/react/src/copybutton/CopyButton.tsx Implements fixed size handling with mergeRefs and inline styles; an extraneous class "tull" is included.
@navikt/core/react/src/copybutton/CopyButton.stories.tsx Adds a story to showcase the layout fix for multi-lined texts.
@navikt/core/css/copybutton.css Adjusts layout properties to align content correctly.
.changeset/ready-points-say.md Updates changeset documentation for the bug fix.
Comments suppressed due to low confidence (1)

@navikt/core/react/src/copybutton/CopyButton.tsx:193

  • [nitpick] The literal 'tull' appears to be an unintended class name. Remove it or replace it with the appropriate class to avoid potential confusion.
          "tull",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants