Comments (2)
Final hook code adapted to my use case -
export function useCollapse({
isExpanded: configExpanded = false,
onExpandStart = noop,
onExpandEnd = noop,
onCollapseStart = noop,
onCollapseEnd = noop,
}: UseCollapseInput = {}) {
const [isExpanded, setExpanded] = useState(configExpanded);
const [styles, setStylesRaw] = useState<CSSProperties>(
isExpanded ? {} : collapsedStyles
);
useEffect(() => {
expandCollapse(configExpanded);
}, [configExpanded]);
const el = useRef<HTMLElement | null>(null);
const setStyles = (newStyles: {} | ((oldStyles: {}) => {})) => {
flushSync(() => {
setStylesRaw(newStyles);
});
};
const mergeStyles = (newStyles: {}) => {
setStyles((oldStyles) => ({ ...oldStyles, ...newStyles }));
};
const handleTransitionEnd = (e: TransitionEvent): void => {
if (e.target !== el.current || e.propertyName !== "height") {
return;
}
if (isExpanded) {
const height = getElementHeight(el);
if (height === styles.height) {
setStyles({});
} else {
mergeStyles({ height });
}
onExpandEnd();
}
onCollapseEnd();
};
function expandCollapse(nextState: boolean) {
setExpanded(nextState);
if (nextState) {
requestAnimationFrame(() => {
onExpandStart();
mergeStyles({
willChange: "height",
display: "block",
overflow: "hidden",
});
requestAnimationFrame(() => {
const height = getElementHeight(el);
mergeStyles({
transition: `height ${duration}ms ${easing}`,
height,
});
});
});
} else {
requestAnimationFrame(() => {
onCollapseStart();
const height = getElementHeight(el);
mergeStyles({
transition: `height ${duration}ms ${easing}`,
willChange: "height",
height,
});
requestAnimationFrame(() => {
mergeStyles({
height: "0px",
overflow: "hidden",
});
});
});
}
}
function getToggleProps({
disabled = false,
onClick = noop,
...rest
}: GetTogglePropsInput = {}): GetTogglePropsOutput {
return {
type: "button",
role: "button",
id: `react-collapsed-toggle`,
"aria-controls": `react-collapsed-panel`,
"aria-expanded": isExpanded,
tabIndex: 0,
disabled,
...rest,
onClick: disabled
? noop
: callAll(onClick, () => {
expandCollapse(!isExpanded);
}),
};
}
function getCollapseProps({
style = {},
onTransitionEnd = noop,
refKey = "ref",
...rest
}: GetCollapsePropsInput = {}): GetCollapsePropsOutput {
const theirRef: any = rest[refKey];
return {
id: `react-collapsed-panel`,
"aria-hidden": !isExpanded,
[refKey]: mergeRefs(el, theirRef),
...rest,
onTransitionEnd: callAll(handleTransitionEnd, onTransitionEnd),
style: {
boxSizing: "border-box",
// additional styles passed, e.g. getCollapseProps({style: {}})
...style,
// style overrides from state
...styles,
},
};
}
return {
getToggleProps,
getCollapseProps,
isExpanded,
setExpanded,
};
}
from collapsed.
The problem with moving it to a click handler is that the expand state won't change when props change, only if the click handler is called with getToggleProps
. This library is meant to be used without it.
This library was refactored to a vanilla JS core (will be soon pushed to latest
), and that changes this whole set-up quite a bit.
from collapsed.
Related Issues (20)
- If the height is changed from the outside while the open/close process has never been executed, it will not synchronize. HOT 5
- Remove raf dependency HOT 1
- Since switching to createRoute (concurrent React), inline styles are sometimes not updated HOT 15
- Dependency Dashboard
- No longer compatible with React < 18. HOT 2
- Use expand on a Table cell on react HOT 2
- New version doesn't toggle HOT 9
- Media query collapsed HOT 2
- Incorrect peer dependency HOT 1
- SSR usage - prevent initially display if not expanded by default HOT 5
- OffsetHeight vs ScrollHeight HOT 3
- hasDisabledAnimation isn't `false` by default HOT 3
- Request to update "react-collapsed" CONTRIBUTING.md due to old version and package manager conversion.
- Safari 13 support, or at least graceful error handling? HOT 3
- Accordion mode HOT 2
- Show one collapsible at a time HOT 1
- Is it possible to have 2 icons switch back and forth when expanding or collapsing? HOT 1
- We should have multiple collapse/accordion option in a single component
- inline styling not working HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from collapsed.