Skip to content

Commit d100cac

Browse files
fix(copybutton): react and wc parity (#18318)
* fix(copybutton): react and wc parity * fix(copybutton): review changes * fix(copybutton): review changes --------- Co-authored-by: kennylam <[email protected]>
1 parent affc44d commit d100cac

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

packages/web-components/src/components/copy-button/copy-button.stories.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,40 @@ import { html } from 'lit';
1111
import { ifDefined } from 'lit/directives/if-defined.js';
1212
import type { Meta } from '@storybook/web-components';
1313
import './copy-button';
14+
import { POPOVER_ALIGNMENT } from '../popover/defs';
15+
16+
const tooltipAlignments = {
17+
[`top`]: POPOVER_ALIGNMENT.TOP,
18+
[`top-start`]: POPOVER_ALIGNMENT.TOP_START,
19+
[`top-end`]: POPOVER_ALIGNMENT.TOP_END,
20+
[`bottom`]: POPOVER_ALIGNMENT.BOTTOM,
21+
[`bottom-start`]: POPOVER_ALIGNMENT.BOTTOM_START,
22+
[`bottom-end`]: POPOVER_ALIGNMENT.BOTTOM_END,
23+
[`left`]: POPOVER_ALIGNMENT.LEFT,
24+
[`left-start`]: POPOVER_ALIGNMENT.LEFT_START,
25+
[`left-end`]: POPOVER_ALIGNMENT.LEFT_END,
26+
[`right`]: POPOVER_ALIGNMENT.RIGHT,
27+
[`right-start`]: POPOVER_ALIGNMENT.RIGHT_START,
28+
[`right-end`]: POPOVER_ALIGNMENT.RIGHT_END,
29+
};
1430

1531
const defaultArgs = {
1632
feedback: 'Copied!',
1733
feedbackTimeout: 2000,
1834
iconDescription: 'Copy to clipboard',
35+
align: POPOVER_ALIGNMENT.BOTTOM,
1936
};
2037

2138
const controls = {
39+
align: {
40+
control: 'select',
41+
description: 'Specify how the toggletip should align with the button',
42+
options: tooltipAlignments,
43+
},
44+
autoAlign: {
45+
control: 'boolean',
46+
description: 'Specify how the toggletip should align with the button',
47+
},
2248
feedback: {
2349
control: 'text',
2450
description: `Provide a description for the icon representing the copy action that can be read by screen readers`,
@@ -35,8 +61,17 @@ const controls = {
3561

3662
const meta: Meta = {
3763
title: 'Components/Copy button',
38-
render: ({ feedbackText, feedbackTimeout, onClick, iconDescription }) => html`
64+
render: ({
65+
feedbackText,
66+
feedbackTimeout,
67+
onClick,
68+
iconDescription,
69+
align,
70+
autoAlign,
71+
}) => html`
3972
<cds-copy-button
73+
align="${align}"
74+
?autoalign="${autoAlign}"
4075
feedback="${ifDefined(feedbackText)}"
4176
feedback-timeout="${ifDefined(feedbackTimeout)}"
4277
@click="${onClick}">
@@ -47,12 +82,6 @@ const meta: Meta = {
4782
};
4883

4984
export const Default = {
50-
parameters: {
51-
controls: { exclude: /(.*?)/ },
52-
},
53-
};
54-
55-
export const Playground = {
5685
argTypes: controls,
5786
};
5887

packages/web-components/src/components/copy-button/copy-button.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { carbonElement as customElement } from '../../globals/decorators/carbon-
1414
import { prefix } from '../../globals/settings';
1515
import FocusMixin from '../../globals/mixins/focus';
1616
import styles from './copy-button.scss?lit';
17+
import { POPOVER_ALIGNMENT } from '../popover/defs';
1718
import '../copy/copy';
1819

1920
/**
@@ -41,14 +42,33 @@ class CDSCopyButton extends FocusMixin(LitElement) {
4142
@property()
4243
feedback = 'Copied!';
4344

45+
/**
46+
* How the tooltip is aligned to the trigger button.
47+
*/
48+
@property({ reflect: true })
49+
align = POPOVER_ALIGNMENT.BOTTOM;
50+
51+
/**
52+
* Specify whether a auto align functionality should be applied
53+
*/
54+
@property({ type: Boolean, reflect: true })
55+
autoAlign = false;
56+
4457
/**
4558
* The number in milliseconds to determine how long the tooltip should remain.
4659
*/
4760
@property({ type: Number, attribute: 'feedback-timeout' })
4861
feedbackTimeout = 2000;
4962

5063
render() {
51-
const { buttonClassName, disabled, feedback, feedbackTimeout } = this;
64+
const {
65+
buttonClassName,
66+
disabled,
67+
feedback,
68+
feedbackTimeout,
69+
align,
70+
autoAlign,
71+
} = this;
5272

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

@@ -59,9 +79,11 @@ class CDSCopyButton extends FocusMixin(LitElement) {
5979
return html`
6080
<cds-copy
6181
?disabled=${disabled}
82+
?autoalign=${autoAlign}
6283
feedback=${feedback}
6384
feedback-timeout=${feedbackTimeout}
64-
button-class-name=${classes}>
85+
button-class-name=${classes}
86+
align=${align}>
6587
${Copy16({ slot: 'icon', class: `${prefix}--snippet__icon` })}
6688
<slot slot="tooltip-content"></slot>
6789
</cds-copy>

packages/web-components/src/components/copy/copy.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ class CDSCopy extends CDSIconButton {
9292

9393
connectedCallback() {
9494
this.closeOnActivation = false;
95-
this.align = 'bottom';
9695

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

0 commit comments

Comments
 (0)