Button

Komponen tombol untuk merender tombol atau tautan

Jika kalian ingin membuat tombol, kalian bisa menggunakan komponen ini

Button.jsx
1import { Twc } from "@/lib/utils"; 2import React, { forwardRef } from "react"; 3 4const baseStyles = "inline-flex items-center justify-center font-medium rounded transition-colors disabled:opacity-50 disabled:pointer-events-none"; 5 6const variantClasses = { 7 filed: "bg-[#0079f8] text-white hover:bg-blue-600 ", 8 default: "bg-white text-gray-900 rounded-md border border-gray-300 hover:border-gray-50", 9 outline: "border bg-transparent border-gray-600 border-2 text-gray-900 rounded-md", 10}; 11 12const sizeClasses = { 13 sm: "px-3 py-1 text-sm", 14 md: "px-4 py-2 text-base", 15 lg: "px-5 py-3 text-lg", 16}; 17 18export type ButtonProps = React.ComponentPropsWithRef<"button"> & { 19 as?: React.ElementType; 20 variant?: keyof typeof variantClasses; 21 size?: keyof typeof sizeClasses; 22 className?: string; 23 children?: React.ReactNode; 24}; 25 26 export const Button: React.FC<ButtonProps> = forwardRef( 27 ( 28 { 29 as: Component = "button", 30 variant = "default", 31 size = "md", 32 className, 33 children, 34 ...props 35 }, 36 ref 37 ) => { 38 return ( 39 <Component 40 ref={ref} 41 className={Twc( 42 baseStyles, 43 variantClasses[variant], 44 sizeClasses[size], 45 className 46 )} 47 {...props} 48 > 49 {children} 50 </Component> 51 ); 52 } 53 ); 54 55 Button.displayName = "Button"; 56
Button.jsx
1const CobaButton = () => { 2 return <Button variant="default"> Default </Button>; 3 };

Keunggulan Table

  • Fully typed dengan dukungan React.ComponentPropsWithRef untuk semua atribut native <button>.
  • 🎨 TailwindCSS-ready, dengan sistem varian visual yang konsisten lewat prop variant dan size.
  • 🔁 Polymorphic melalui prop as, sehingga bisa digunakan sebagai <a>, <div>, atau elemen lainnya dengan tampilan tombol.
  • 🧠 Ref-friendly, mendukung penggunaan ref untuk integrasi dengan fokus manual, animasi, atau akses imperative lainnya.
  • 💡 Kelas CSS terorganisir menggunakan helper Twc (gabungan dari clsx + twMerge), mencegah duplikasi dan konflik class.
  • 🛡️ Reusable dan maintainable, siap digunakan dalam berbagai konteks UI baik untuk form, navigasi, maupun aksi sekunder.