Files
supabase/apps/docs/components/Table.tsx
Francesco Sansalvadore 407501b16c fix docs table overflow issues (#27322)
* fix table overflow issues

* w-full on table container

* refactor Table component

* fix table type

* cleanup

* removeEventListener
2024-06-18 18:25:08 +02:00

41 lines
1.2 KiB
TypeScript

import React, { TableHTMLAttributes, useEffect, useRef, useState } from 'react'
import { cn } from 'ui'
type TableProps = TableHTMLAttributes<HTMLTableElement>
const Table = ({ children, ...props }: TableProps) => {
const containerRef = useRef(null)
const [showShadow, setShowShadow] = useState(true)
const handleScroll = () => {
const container = containerRef.current
if (container) {
const { scrollWidth, scrollLeft, offsetWidth } = container
const isAtEnd = scrollWidth - scrollLeft - 2 < offsetWidth
setShowShadow(!isAtEnd)
}
}
useEffect(() => {
containerRef?.current?.addEventListener('scroll', handleScroll)
return () => containerRef?.current?.removeEventListener('scroll', handleScroll)
}, [])
return (
<div className="relative">
<span
className={cn(
'block md:hidden absolute inset-0 left-auto w-5 bg-gradient-to-r from-transparent to-background transition-opacity opacity-100',
!showShadow && 'opacity-0 duration-300'
)}
/>
<div ref={containerRef} className="w-full overflow-x-auto break-normal">
<table {...props}>{children}</table>
</div>
</div>
)
}
export default Table