Skip to content
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

Merged
merged 3 commits into from
Apr 25, 2024

Conversation

deatiger
Copy link
Collaborator

@deatiger deatiger commented Apr 25, 2024

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)

  • Export the component in src/components/index.ts
  • Add example to .storybook/documents/Information/Samples/Samples.stories.tsx
  • Localize added component

Copy link

changeset-bot bot commented Apr 25, 2024

🦋 Changeset detected

Latest commit: d5a4db4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
ingred-ui Minor

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

Copy link

netlify bot commented Apr 25, 2024

Deploy Preview for ingred-ui ready!

Name Link
🔨 Latest commit d5a4db4
🔍 Latest deploy log https://app.netlify.com/sites/ingred-ui/deploys/6629a30a6f9a530008997d42
😎 Deploy Preview https://deploy-preview-1605--ingred-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Comment on lines -29 to -36
const height = useMemo(() => {
if (expandedState && accordionContentContainerRef.current) {
return accordionContentContainerRef.current.scrollHeight;
}
return 0;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [expandedState, accordionContentContainerRef.current]);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今までは useMemo で計算していたが、後述する onTransitionEnd で height を変更したかったので state で管理するようにした。

Comment on lines +33 to +34
if (expanded) {
setHeight(`${accordionContentContainerRef.current?.scrollHeight ?? 0}px`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accordion を開く時には、height の値が 0 → コンテンツの高さ になることで開く際のアニメーションを実現している。

Comment on lines +35 to +39
} else {
setHeight(`${accordionContentContainerRef.current?.scrollHeight ?? 0}px`);
setOverflow("hidden");
setTimeout(() => setHeight("0px"), 100);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下のような height の変更を行うことで閉じるアニメーションを実現している( auto → 0px ではアニメーションしないため)

  1. auto → コンテンツの高さに変更
  2. state が更新されるのを待つために少し wait を入れてから、0px に変更

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど!
閉じてる時は0で開いてる時はautoの2値じゃダメなのかなーと思ったんですが、アニメーションを行うために > 0px の高さを持てるようにしてるんですね。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ですです!(cssめんどくせえと思ってしまった)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

次世代の革命的なcss出て欲しい

Comment on lines +52 to +57
const handleTransitionEnd = () => {
if (expandedState) {
setHeight("auto");
setOverflow("visible");
}
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

アコーディオンが開き終わったら height を auto にすることで、その後アコーディオン内のコンテンツが増減した場合に動的にアコーディオンの高さも変わる。

Copy link

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}
Copy link
Collaborator Author

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[memo]アニメーションが終わったら高さをautoにして、visibleにする。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ていうかこんなイベントあったんだ……。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2年ぶりくらいに使いました笑

@deatiger deatiger requested a review from noronaoki April 25, 2024 00:32
@NAKKA-K
Copy link

NAKKA-K commented Apr 25, 2024

コード的にはOKです!

@noronaoki
Copy link
Contributor

@deatiger LGTM!

@deatiger deatiger merged commit 3053562 into master Apr 25, 2024
3 checks passed
@deatiger deatiger deleted the dynamic-height-accordion branch April 25, 2024 00:53
@FluctMember FluctMember mentioned this pull request Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants