@wonderflow/react-components
Version:
UI components from Wonderflow's Wanda design system
36 lines (35 loc) • 2.33 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/*
* Copyright 2022-2023 Wonderflow Design Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import clsx from 'clsx';
import { domMax, LazyMotion, m, } from 'framer-motion';
import { useCallback, useState } from 'react';
import { useUIDSeed } from 'react-uid';
import { Button, Elevator, Stack } from '../..';
import * as styles from './expander.module.css';
export const Expander = ({ expandLabel = 'Show more', collapseLabel = 'Show less', visibleArea = '100px', defaultExpanded = false, className, children, style, ...otherProps }) => {
const [isCollapsed, setIsCollapsed] = useState(!defaultExpanded);
const uid = useUIDSeed();
const dynamicStyle = {
'--visibleArea': visibleArea,
};
const handleCollapse = useCallback(() => {
setIsCollapsed(s => !s);
}, []);
return (_jsxs("div", { style: { ...dynamicStyle, style }, className: clsx(styles.Expander, className), "data-expander-collapsed": isCollapsed, ...otherProps, children: [_jsx(LazyMotion, { features: domMax, children: _jsx(m.div, { className: styles.Content, id: uid('expander-content'), animate: !isCollapsed ? { height: 'auto' } : {
height: 'calc(var(--visibleArea) + var(--threshold))', overflow: 'hidden',
}, transition: { ease: 'easeOut', duration: 0.2 }, initial: false, children: children }) }), _jsx(Stack, { className: styles.Action, hAlign: "center", vAlign: "center", fill: false, children: _jsx(Elevator, { resting: isCollapsed ? 4 : 0, children: _jsx(Button, { iconPosition: "right", "data-testid": "ExpanderButton", "aria-expanded": !isCollapsed, "aria-controls": uid('expander-content'), onClick: handleCollapse, children: isCollapsed ? expandLabel : collapseLabel }) }) })] }));
};