Files
test-repo/website/playground/buttons.jsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.5 KiB
React
Raw Normal View History

2025-12-30 14:14:17 +08:00
import ClipboardJS from "clipboard";
2025-12-10 23:48:10 +08:00
import { onMounted, reactive, ref } from "vue";
2018-04-12 13:27:34 -03:00
2025-12-10 23:48:10 +08:00
export const Button = (props, { slots, attrs }) => (
<button type="button" class="btn" {...attrs}>
{slots.default()}
</button>
);
2018-04-12 15:09:04 -03:00
2025-12-10 23:48:10 +08:00
function setup(props, { slots, attrs }) {
const state = reactive({ showTooltip: false, tooltipText: "" });
let timer = null;
const buttonRef = ref();
const componentDidMount = () => {
const clipboard = new ClipboardJS(buttonRef.value, {
text() {
const { copy } = props;
2018-04-17 13:40:55 -03:00
return typeof copy === "function" ? copy() : copy;
},
2018-04-12 15:09:04 -03:00
});
2025-12-10 23:48:10 +08:00
clipboard.on("success", () => showTooltip("Copied!"));
clipboard.on("error", () => showTooltip("Press ctrl+c to copy"));
};
2018-04-12 15:09:04 -03:00
2025-12-10 23:48:10 +08:00
const showTooltip = (text) => {
Object.assign(state, { showTooltip: true, tooltipText: text });
2018-04-12 15:09:04 -03:00
2025-12-10 23:48:10 +08:00
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
timer = null;
Object.assign(state, { showTooltip: false });
}, 2000);
};
const render = () => {
const { showTooltip, tooltipText } = state;
const children = slots.default();
2018-04-12 15:09:04 -03:00
return (
2025-12-10 23:48:10 +08:00
<Button ref={buttonRef} {...attrs}>
{showTooltip ? <span class="tooltip">{tooltipText}</span> : null}
{children}
2018-04-12 15:09:04 -03:00
</Button>
);
2025-12-10 23:48:10 +08:00
};
onMounted(componentDidMount);
return render;
2018-04-12 13:27:34 -03:00
}
2025-12-10 23:48:10 +08:00
export const ClipboardButton = {
name: "ClipboardButton",
props: {
copy: { type: [String, Function], required: true },
},
setup,
};