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
|
|
|
const { ClipboardJS } = window;
|
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}
|
2019-08-30 23:30:36 +03:00
|
|
|
{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,
|
|
|
|
|
};
|