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(copybutton): react and wc parity #18318

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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 Down
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
5 changes: 4 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,10 @@ class CDSCopy extends CDSIconButton {

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

this.align = this.align || 'bottom';

this.autoalign = this.autoalign || false;

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

Expand Down
Loading