material-you-react/src/primitive-components/button-layout/button-layout.tsx

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-02-01 00:58:19 +03:00
'use client';
2024-02-01 15:23:59 +03:00
import { bool, string } from 'prop-types';
2024-02-01 00:58:19 +03:00
import { RippleArea } from '../ripple/ripple-area';
2024-02-01 15:23:59 +03:00
import { ButtonLayoutProps } from './button-layout.types';
2024-02-01 00:58:19 +03:00
import useRippleEffect from '../ripple/hooks/useRippleEffect';
2024-02-01 15:23:59 +03:00
import React, { forwardRef, useId, useRef, useState } from 'react';
2024-02-01 00:58:19 +03:00
2024-02-01 15:23:59 +03:00
export const ButtonLayout = forwardRef<HTMLButtonElement, ButtonLayoutProps>(
2024-02-02 13:39:02 +03:00
({ centralRipple = false, ...props }, ref) => {
2024-02-01 15:23:59 +03:00
const [isActive, setIsActive] = useState<boolean>(false),
ripplesRef = useRef(null),
buttonId = useId(),
events = useRippleEffect(ripplesRef, setIsActive);
2024-02-01 00:58:19 +03:00
2024-02-02 13:39:02 +03:00
const classes =
`m3${isActive ? ' is-active' : ''} ${props.className ?? ''}`.trimEnd();
2024-02-01 00:58:19 +03:00
2024-02-01 15:23:59 +03:00
return (
<button
{...props}
{...events}
className={classes}
2024-02-01 21:24:37 +03:00
disabled={props.disabled}
2024-02-01 15:23:59 +03:00
id={buttonId}
ref={ref}
>
{props.children}
<RippleArea
callback={setIsActive}
central={centralRipple}
ref={ripplesRef}
/>
</button>
);
},
);
2024-02-01 00:58:19 +03:00
2024-02-01 15:23:59 +03:00
ButtonLayout.propTypes = {
children: string,
2024-02-02 13:39:02 +03:00
centralRipple: bool,
2024-02-01 15:23:59 +03:00
};