mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-29 23:55:02 +00:00
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
"use client"
|
|
|
|
import Script from "next/script"
|
|
import { Button } from "@/examples/base/ui/button"
|
|
import { DiceFaces05Icon } from "@hugeicons/core-free-icons"
|
|
import { HugeiconsIcon } from "@hugeicons/react"
|
|
|
|
import { cn } from "@/lib/utils"
|
|
import { useRandom } from "@/app/(create)/hooks/use-random"
|
|
|
|
export const RANDOMIZE_FORWARD_TYPE = "randomize-forward"
|
|
|
|
export function RandomButton({
|
|
variant = "outline",
|
|
className,
|
|
...props
|
|
}: React.ComponentProps<typeof Button>) {
|
|
const { randomize } = useRandom()
|
|
|
|
return (
|
|
<Button
|
|
variant={variant}
|
|
onClick={randomize}
|
|
className={cn(
|
|
"touch-manipulation bg-transparent! px-2! py-0! text-sm! transition-none select-none hover:bg-muted! pointer-coarse:h-10!",
|
|
className
|
|
)}
|
|
{...props}
|
|
>
|
|
<span className="w-full text-center font-medium">Shuffle</span>
|
|
</Button>
|
|
)
|
|
}
|
|
|
|
export function RandomizeScript() {
|
|
return (
|
|
<Script
|
|
id="randomize-listener"
|
|
strategy="beforeInteractive"
|
|
dangerouslySetInnerHTML={{
|
|
__html: `
|
|
(function() {
|
|
// Forward R key
|
|
document.addEventListener('keydown', function(e) {
|
|
if ((e.key === 'r' || e.key === 'R') && !e.metaKey && !e.ctrlKey) {
|
|
if (
|
|
(e.target instanceof HTMLElement && e.target.isContentEditable) ||
|
|
e.target instanceof HTMLInputElement ||
|
|
e.target instanceof HTMLTextAreaElement ||
|
|
e.target instanceof HTMLSelectElement
|
|
) {
|
|
return;
|
|
}
|
|
e.preventDefault();
|
|
if (window.parent && window.parent !== window) {
|
|
window.parent.postMessage({
|
|
type: '${RANDOMIZE_FORWARD_TYPE}',
|
|
key: e.key
|
|
}, '*');
|
|
}
|
|
}
|
|
});
|
|
|
|
})();
|
|
`,
|
|
}}
|
|
/>
|
|
)
|
|
}
|