Skip to content

refactor(LineClamp): childrenを依存配列から削除しResizeObserverを使用#6406

Open
AtsushiM wants to merge 2 commits into
masterfrom
refactor/line-clamp-remove-children-from-deps
Open

refactor(LineClamp): childrenを依存配列から削除しResizeObserverを使用#6406
AtsushiM wants to merge 2 commits into
masterfrom
refactor/line-clamp-remove-children-from-deps

Conversation

@AtsushiM

Copy link
Copy Markdown
Member

関連URL

なし

概要

eslint-config-smarthr@14.7.0で追加されたsmarthr/best-practice-for-unstable-dependenciesルールにより、useEffectの依存配列に不安定な参照であるchildrenが含まれていることが警告されるようになりました。

childrenは参照が頻繁に変わるため、依存配列に含めると不要な再実行や無限ループの原因となる可能性があります。

変更内容

  • useEffectの依存配列からchildrenを削除
  • ResizeObserverを追加して表示要素(ref)と高さ計測用要素(shadowRef)のサイズ変更を監視
  • 内容が変わってサイズが変わった際も、ResizeObserverが検知して適切に再計算

Before

useEffect(() => {
  const el = ref.current
  const shadowEl = shadowRef.current

  const isMultiLineOverflow = el && shadowEl ? shadowEl.clientHeight > el.clientHeight : false

  setTooltipVisible(isMultiLineOverflow)
}, [maxLines, children]) // childrenが不安定な参照

After

useEffect(() => {
  const el = ref.current
  const shadowEl = shadowRef.current

  if (!el || !shadowEl) return

  const checkOverflow = () => {
    const isMultiLineOverflow = shadowEl.clientHeight > el.clientHeight
    setTooltipVisible(isMultiLineOverflow)
  }

  checkOverflow()

  const resizeObserver = new ResizeObserver(checkOverflow)
  resizeObserver.observe(el)
  resizeObserver.observe(shadowEl)

  return () => {
    resizeObserver.unobserve(el)
    resizeObserver.unobserve(shadowEl)
  }
}, [maxLines]) // childrenを削除

プロダクト側で対応が必要な事項

なし

確認方法

  • 既存のテストが通ることを確認
  • Storybookで行数制限されたテキストの動作を確認
  • 内容が動的に変わる場合のツールチップ表示を確認

useEffectの依存配列にchildrenが含まれていると、不安定な参照のため
頻繁に再実行される問題がありました。

ResizeObserverを使用して表示要素と高さ計測用要素のサイズ変更を監視することで、
内容が変わってサイズが変わった際も適切に検知しつつ、
不要な再実行を防ぎます。

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@pkg-pr-new

pkg-pr-new Bot commented Jun 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@smarthr/smarthr-ui-charts@6406
npm i https://pkg.pr.new/smarthr-ui@6406

commit: d6faede

- checkOverflow内で直接setTooltipVisibleに値を渡すように変更

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@AtsushiM AtsushiM marked this pull request as ready for review June 19, 2026 05:13
@AtsushiM AtsushiM requested a review from a team as a code owner June 19, 2026 05:13
@AtsushiM AtsushiM requested review from saitolume and uknmr and removed request for a team June 19, 2026 05:13
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.

1 participant