-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
♻️ Accordion 内部のコンテンツの高さが動的に変わる場合に対応 #1605
Conversation
🦋 Changeset detectedLatest commit: d5a4db4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for ingred-ui ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
const height = useMemo(() => { | ||
if (expandedState && accordionContentContainerRef.current) { | ||
return accordionContentContainerRef.current.scrollHeight; | ||
} | ||
return 0; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [expandedState, accordionContentContainerRef.current]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
今までは useMemo で計算していたが、後述する onTransitionEnd で height を変更したかったので state で管理するようにした。
if (expanded) { | ||
setHeight(`${accordionContentContainerRef.current?.scrollHeight ?? 0}px`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accordion を開く時には、height の値が 0 → コンテンツの高さ になることで開く際のアニメーションを実現している。
} else { | ||
setHeight(`${accordionContentContainerRef.current?.scrollHeight ?? 0}px`); | ||
setOverflow("hidden"); | ||
setTimeout(() => setHeight("0px"), 100); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下のような height の変更を行うことで閉じるアニメーションを実現している( auto → 0px ではアニメーションしないため)
- auto → コンテンツの高さに変更
- state が更新されるのを待つために少し wait を入れてから、0px に変更
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど!
閉じてる時は0で開いてる時はautoの2値じゃダメなのかなーと思ったんですが、アニメーションを行うために > 0px の高さを持てるようにしてるんですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ですです!(cssめんどくせえと思ってしまった)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
次世代の革命的なcss出て欲しい
const handleTransitionEnd = () => { | ||
if (expandedState) { | ||
setHeight("auto"); | ||
setOverflow("visible"); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
アコーディオンが開き終わったら height を auto にすることで、その後アコーディオン内のコンテンツが増減した場合に動的にアコーディオンの高さも変わる。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
開く時以外はアニメーション必要ないからautoでOKということですね。
height={height} | ||
overflow={overflow} | ||
onTransitionEnd={handleTransitionEnd} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CSSトランジション(アニメーション)が終わる際に発火されるイベント
https://developer.mozilla.org/ja/docs/Web/API/Element/transitionend_event
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[memo]アニメーションが終わったら高さをautoにして、visibleにする。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ていうかこんなイベントあったんだ……。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2年ぶりくらいに使いました笑
コード的にはOKです! |
@deatiger LGTM! |
Preview URL
https://deploy-preview-1605--ingred-ui.netlify.app/?path=/docs/components-navigation-accordion--docs
Check List (If️ you added new component in this PR)
src/components/index.ts
.storybook/documents/Information/Samples/Samples.stories.tsx