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

TooltipButton: Converted component to TypeScript #2317

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/strange-schools-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hashicorp/design-system-components": minor
---

`TooltipButton`: Converted component to TypeScript
2 changes: 2 additions & 0 deletions packages/components/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import HdsTextBody from './components/hds/text/body.ts';
import HdsTextCode from './components/hds/text/code.ts';
import HdsTextDisplay from './components/hds/text/display.ts';
import HdsToast from './components/hds/toast/index.ts';
import HdsTooltipButton from './components/hds/tooltip-button/index.ts';

export {
HdsAccordion,
Expand Down Expand Up @@ -196,4 +197,5 @@ export {
HdsTextCode,
HdsTextDisplay,
HdsToast,
HdsTooltipButton,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@
import Component from '@glimmer/component';
import { assert } from '@ember/debug';

export const PLACEMENTS = [
'top',
'top-start',
'top-end',
'right',
'right-start',
'right-end',
'bottom',
'bottom-start',
'bottom-end',
'left',
'left-start',
'left-end',
];
import type { Props as TippyProps } from 'tippy.js';
import { HdsTooltipPlacementValues } from './types.ts';

export default class HdsTooltipIndexComponent extends Component {
export const PLACEMENTS: string[] = Object.values(HdsTooltipPlacementValues);

export interface HdsTooltipIndexSignature {
Args: {
extraTippyOptions: Omit<TippyProps, 'placement' | 'offset'>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: since placement and offset will always get overwritten on line 53, I figure we just omit them from the type to avoid confusion.

isInline?: boolean;
offset?: [number, number];
placement: HdsTooltipPlacementValues;
text: string;
};
Blocks: {
default: [];
};
Element: HTMLButtonElement;
}

export default class HdsTooltipIndexComponent extends Component<HdsTooltipIndexSignature> {
/**
* @param text
* @type {string}
* @description text content for tooltip
*/
get text() {
let { text } = this.args;
get text(): string {
const { text } = this.args;

assert(
'@text for "Hds::TooltipButton" must have a valid value',
Expand All @@ -38,8 +42,8 @@ export default class HdsTooltipIndexComponent extends Component {
return text;
}

get options() {
let { placement } = this.args;
get options(): TippyProps {
const { placement } = this.args;

assert(
'@placement for "Hds::TooltipButton" must have a valid value',
Expand All @@ -48,10 +52,10 @@ export default class HdsTooltipIndexComponent extends Component {

return {
...this.args.extraTippyOptions,
// takes string
placement: placement,
// takes array of 2 numbers (skidding, distance): array(0, 0)
offset: this.args.offset,
showOnCreate: true,
// takes array of 2 numbers (skidding, distance): array(0, 10)
offset: this.args.offset ? this.args.offset : [0, 10],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: turns out the default if offset is not provided is [0,10] not [0,0]

https://github.com/atomiks/tippyjs/blob/ad85f6feb79cf6c5853c43bf1b2a50c4fa98e7a1/src/props.ts#L44

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch! 👏

};
}

Expand All @@ -61,8 +65,8 @@ export default class HdsTooltipIndexComponent extends Component {
* @default true
* @description sets display for the button
*/
get isInline() {
let { isInline = true } = this.args;
get isInline(): boolean {
const { isInline = true } = this.args;
return isInline;
}

Expand All @@ -71,8 +75,8 @@ export default class HdsTooltipIndexComponent extends Component {
* @method classNames
* @return {string} The "class" attribute to apply to the component.
*/
get classNames() {
let classes = ['hds-tooltip-button'];
get classNames(): string {
const classes = ['hds-tooltip-button'];

// add a class based on the @isInline argument
if (this.isInline) {
Expand Down
16 changes: 16 additions & 0 deletions packages/components/src/components/hds/tooltip-button/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum HdsTooltipPlacementValues {
Top = 'top',
TopStart = 'top-start',
TopEnd = 'top-end',
Right = 'right',
RightStart = 'right-start',
RightEnd = 'right-end',
Bottom = 'bottom',
BottomStart = 'bottom-start',
BottomEnd = 'bottom-end',
Left = 'left',
LeftStart = 'left-start',
LeftEnd = 'left-end',
}

export type HdsTooltipPlacements = `${HdsTooltipPlacementValues}`;
5 changes: 5 additions & 0 deletions packages/components/src/template-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ import type HdsTextComponent from './components/hds/text';
import type HdsTextBodyComponent from './components/hds/text/body';
import type HdsTextDisplayComponent from './components/hds/text/display';
import type HdsTagComponent from './components/hds/tag';
import type HdsTooltipButtonComponent from './components/hds/tooltip-button';
import type HdsToastComponent from './components/hds/toast';
import type HdsTextCodeComponent from './components/hds/text/code';
import type HdsYieldComponent from './components/hds/yield';
Expand Down Expand Up @@ -683,6 +684,10 @@ export default interface HdsComponentsRegistry {
'Hds::Tag': typeof HdsTagComponent;
'hds/tag': typeof HdsTagComponent;

// TooltipButton
'Hds::TooltipButton': typeof HdsTooltipButtonComponent;
'hds/tooltip-button': typeof HdsTooltipButtonComponent;

// Toast
'Hds::Toast': typeof HdsToastComponent;
'hds/toast': typeof HdsToastComponent;
Expand Down
Loading