Skip to content

Commit

Permalink
fix(copybutton): react and wc parity (#18318)
Browse files Browse the repository at this point in the history
* fix(copybutton): react and wc parity

* fix(copybutton): review changes

* fix(copybutton): review changes

---------

Co-authored-by: kennylam <909118+kennylam@users.noreply.github.com>
  • Loading branch information
anamikaanu96 and kennylam authored Feb 11, 2025
1 parent affc44d commit d100cac
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,40 @@ import { html } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import type { Meta } from '@storybook/web-components';
import './copy-button';
import { POPOVER_ALIGNMENT } from '../popover/defs';

const tooltipAlignments = {
[`top`]: POPOVER_ALIGNMENT.TOP,
[`top-start`]: POPOVER_ALIGNMENT.TOP_START,
[`top-end`]: POPOVER_ALIGNMENT.TOP_END,
[`bottom`]: POPOVER_ALIGNMENT.BOTTOM,
[`bottom-start`]: POPOVER_ALIGNMENT.BOTTOM_START,
[`bottom-end`]: POPOVER_ALIGNMENT.BOTTOM_END,
[`left`]: POPOVER_ALIGNMENT.LEFT,
[`left-start`]: POPOVER_ALIGNMENT.LEFT_START,
[`left-end`]: POPOVER_ALIGNMENT.LEFT_END,
[`right`]: POPOVER_ALIGNMENT.RIGHT,
[`right-start`]: POPOVER_ALIGNMENT.RIGHT_START,
[`right-end`]: POPOVER_ALIGNMENT.RIGHT_END,
};

const defaultArgs = {
feedback: 'Copied!',
feedbackTimeout: 2000,
iconDescription: 'Copy to clipboard',
align: POPOVER_ALIGNMENT.BOTTOM,
};

const controls = {
align: {
control: 'select',
description: 'Specify how the toggletip should align with the button',
options: tooltipAlignments,
},
autoAlign: {
control: 'boolean',
description: 'Specify how the toggletip should align with the button',
},
feedback: {
control: 'text',
description: `Provide a description for the icon representing the copy action that can be read by screen readers`,
Expand All @@ -35,8 +61,17 @@ const controls = {

const meta: Meta = {
title: 'Components/Copy button',
render: ({ feedbackText, feedbackTimeout, onClick, iconDescription }) => html`
render: ({
feedbackText,
feedbackTimeout,
onClick,
iconDescription,
align,
autoAlign,
}) => html`
<cds-copy-button
align="${align}"
?autoalign="${autoAlign}"
feedback="${ifDefined(feedbackText)}"
feedback-timeout="${ifDefined(feedbackTimeout)}"
@click="${onClick}">
Expand All @@ -47,12 +82,6 @@ const meta: Meta = {
};

export const Default = {
parameters: {
controls: { exclude: /(.*?)/ },
},
};

export const Playground = {
argTypes: controls,
};

Expand Down
26 changes: 24 additions & 2 deletions packages/web-components/src/components/copy-button/copy-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { carbonElement as customElement } from '../../globals/decorators/carbon-
import { prefix } from '../../globals/settings';
import FocusMixin from '../../globals/mixins/focus';
import styles from './copy-button.scss?lit';
import { POPOVER_ALIGNMENT } from '../popover/defs';
import '../copy/copy';

/**
Expand Down Expand Up @@ -41,14 +42,33 @@ class CDSCopyButton extends FocusMixin(LitElement) {
@property()
feedback = 'Copied!';

/**
* How the tooltip is aligned to the trigger button.
*/
@property({ reflect: true })
align = POPOVER_ALIGNMENT.BOTTOM;

/**
* Specify whether a auto align functionality should be applied
*/
@property({ type: Boolean, reflect: true })
autoAlign = false;

/**
* The number in milliseconds to determine how long the tooltip should remain.
*/
@property({ type: Number, attribute: 'feedback-timeout' })
feedbackTimeout = 2000;

render() {
const { buttonClassName, disabled, feedback, feedbackTimeout } = this;
const {
buttonClassName,
disabled,
feedback,
feedbackTimeout,
align,
autoAlign,
} = this;

let classes = `${prefix}--copy-btn`;

Expand All @@ -59,9 +79,11 @@ class CDSCopyButton extends FocusMixin(LitElement) {
return html`
<cds-copy
?disabled=${disabled}
?autoalign=${autoAlign}
feedback=${feedback}
feedback-timeout=${feedbackTimeout}
button-class-name=${classes}>
button-class-name=${classes}
align=${align}>
${Copy16({ slot: 'icon', class: `${prefix}--snippet__icon` })}
<slot slot="tooltip-content"></slot>
</cds-copy>
Expand Down
1 change: 0 additions & 1 deletion packages/web-components/src/components/copy/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class CDSCopy extends CDSIconButton {

connectedCallback() {
this.closeOnActivation = false;
this.align = 'bottom';

this.addEventListener('click', this._handleClickButton);

Expand Down

0 comments on commit d100cac

Please sign in to comment.