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

CopySnippet: convert to TypeScript (HDS-2689) #2110

Merged
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
5 changes: 5 additions & 0 deletions .changeset/small-boats-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hashicorp/design-system-components": minor
---

`CopySnippet` - Converted component to TypeScript
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{{! @glint-nocheck: not typesafe yet }}
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
Expand All @@ -11,7 +10,7 @@
...attributes
>
<Hds::Text::Code class="hds-copy-snippet__text" @tag="span" @size="100">
{{@textToCopy}}
{{this.textToShow}}
</Hds::Text::Code>
<FlightIcon @name={{this.icon}} class="hds-copy-snippet__icon" />
</button>
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,47 @@ import Component from '@glimmer/component';
import { assert } from '@ember/debug';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { HdsCopySnippetColorValues } from './types.ts';
import type { HdsCopySnippetColors } from './types.ts';
import type { HdsClipboardModifierSignature } from '../../../../modifiers/hds-clipboard.ts';

export const DEFAULT_COLOR = HdsCopySnippetColorValues.Primary;
export const COLORS: string[] = Object.values(HdsCopySnippetColorValues);

export const DEFAULT_COLOR = 'primary';
export const COLORS = ['primary', 'secondary'];
export const DEFAULT_ICON = 'clipboard-copy';
export const SUCCESS_ICON = 'clipboard-checked';
export const ERROR_ICON = 'clipboard-x';
export const DEFAULT_STATUS = 'idle';

export default class HdsCopySnippetIndexComponent extends Component {
interface HdsCopySnippetSignature {
Args: {
KristinLBradley marked this conversation as resolved.
Show resolved Hide resolved
color?: HdsCopySnippetColors;
isFullWidth?: boolean;
textToCopy: HdsClipboardModifierSignature['Args']['Named']['text'];
isTruncated?: boolean;
onSuccess?: HdsClipboardModifierSignature['Args']['Named']['onSuccess'];
onError?: HdsClipboardModifierSignature['Args']['Named']['onError'];
};
Element: HTMLButtonElement;
}

export default class HdsCopySnippetComponent extends Component<HdsCopySnippetSignature> {
@tracked status = DEFAULT_STATUS;
@tracked timer;
@tracked timer: ReturnType<typeof setTimeout> | undefined;

/**
* @param textToCopy
* @type {string | number | bigint | undefined} ???
*/
get textToShow() {
const { textToCopy = '' } = this.args;

if (typeof textToCopy === 'string') {
return textToCopy;
} else {
return textToCopy.toString();
}
}

/**
* @param icon
Expand All @@ -42,7 +72,7 @@ export default class HdsCopySnippetIndexComponent extends Component {
* @description Determines the color of button to be used; acceptable values are `primary` and `secondary`
*/
get color() {
let { color = DEFAULT_COLOR } = this.args;
const { color = DEFAULT_COLOR } = this.args;

assert(
`@color for "Hds::Copy::Snippet" must be one of the following: ${COLORS.join(
Expand Down Expand Up @@ -80,7 +110,7 @@ export default class HdsCopySnippetIndexComponent extends Component {
* @return {string} The "class" attribute to apply to the component.
*/
get classNames() {
let classes = ['hds-copy-snippet'];
const classes = ['hds-copy-snippet'];

// add a class based on the @color argument
classes.push(`hds-copy-snippet--color-${this.color}`);
Expand All @@ -102,23 +132,23 @@ export default class HdsCopySnippetIndexComponent extends Component {
}

@action
onSuccess(args) {
onSuccess(args: HdsClipboardModifierSignature['Args']['Named']['onSuccess']) {
this.status = 'success';
this.resetStatusDelayed();

let { onSuccess } = this.args;
const { onSuccess } = this.args;

if (typeof onSuccess === 'function') {
onSuccess(args);
}
}

@action
onError(args) {
onError(args: HdsClipboardModifierSignature['Args']['Named']['onError']) {
this.status = 'error';
this.resetStatusDelayed();

let { onError } = this.args;
const { onError } = this.args;

if (typeof onError === 'function') {
onError(args);
Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/components/hds/copy/snippet/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

export enum HdsCopySnippetColorValues {
Primary = 'primary',
Secondary = 'secondary',
}
export type HdsCopySnippetColors = `${HdsCopySnippetColorValues}`;
5 changes: 5 additions & 0 deletions packages/components/src/template-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type HdsAppFrameModalsComponent from './components/hds/app-frame/parts/mo
import type HdsAppFrameSidebarComponent from './components/hds/app-frame/parts/sidebar';
import type HdsCardContainerComponent from './components/hds/card/container.ts';
import type HdsCopyButtonComponent from './components/hds/copy/button/index';
import type HdsCopySnippetComponent from './components/hds/copy/snippet';
import type HdsDisclosurePrimitiveComponent from './components/hds/disclosure-primitive';
import type HdsDismissButtonComponent from './components/hds/dismiss-button';
import type HdsIconTileComponent from './components/hds/icon-tile';
Expand Down Expand Up @@ -134,6 +135,10 @@ export default interface HdsComponentsRegistry {
'Hds::Copy::Button': typeof HdsCopyButtonComponent;
'hds/copy/button': typeof HdsCopyButtonComponent;

// Copy Snippet
'Hds::Copy::Snippet': typeof HdsCopySnippetComponent;
'hds/copy/snippet': typeof HdsCopySnippetComponent;

// Disclosure Primitive
'Hds::DisclosurePrimitive': typeof HdsDisclosurePrimitiveComponent;
'hds/disclosure-primitive': typeof HdsDisclosurePrimitiveComponent;
Expand Down