-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularButton.ts
71 lines (65 loc) · 2.08 KB
/
circularButton.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { getResourceUrl, onInteraction } from "../utils/index.js";
import { createRipple } from "./ripple.js";
import type { ResourceKey } from "../types.js";
type CircularBtnOptions = {
/** Tooltip and aria-label of the button */
title: string;
/** Whether the button should have a ripple effect - defaults to true */
ripple?: boolean;
} & (
| {
/** Resource key for the button icon */
resourceName: (ResourceKey & `icon-${string}`) | "_";
}
| {
src: string | Promise<string>;
}
) & (
| {
/** URL to navigate to when the button is clicked */
href: string;
}
| {
/** Callback function to execute when the button is clicked */
onClick: (event: MouseEvent | KeyboardEvent) => void;
}
);
/**
* Creates a generic, circular button element.
* If `href` is provided, the button will be an anchor element.
* If `onClick` is provided, the button will be a div element.
* Provide either `resourceName` or `src` to specify the icon inside the button.
*/
export async function createCircularBtn({
title,
ripple = true,
...rest
}: CircularBtnOptions) {
let btnElem: HTMLElement;
if("href" in rest && rest.href) {
btnElem = document.createElement("a");
(btnElem as HTMLAnchorElement).href = rest.href;
btnElem.role = "button";
(btnElem as HTMLAnchorElement).target = "_blank";
(btnElem as HTMLAnchorElement).rel = "noopener noreferrer";
}
else if("onClick" in rest && rest.onClick) {
btnElem = document.createElement("div");
rest.onClick && onInteraction(btnElem, rest.onClick);
}
else
throw new TypeError("Either 'href' or 'onClick' must be provided");
btnElem.classList.add("bytm-generic-btn");
btnElem.ariaLabel = btnElem.title = title;
btnElem.tabIndex = 0;
btnElem.role = "button";
const imgElem = document.createElement("img");
imgElem.classList.add("bytm-generic-btn-img");
imgElem.src = "src" in rest
? rest.src instanceof Promise
? await rest.src
: rest.src
: await getResourceUrl(rest.resourceName);
btnElem.appendChild(imgElem);
return ripple ? createRipple(btnElem) : btnElem;
}