Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/search-widget/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,22 @@
</style>
</head>
<body>
<button>启动</button>
<button id="color">暗色/亮色</button>
<h1>Search Widget - Page Jitter Fix Test</h1>
<button>Open Search Modal</button>
<button id="color">Toggle Dark/Light Mode</button>

<div style="margin-top: 2rem;">
<h2>Testing the Fix</h2>
<p>This page has enough content to show a scrollbar. When you open the search modal, the page should NOT shift to the right.</p>
<p>The fix works by calculating the scrollbar width and adding equivalent padding to compensate for the scrollbar removal.</p>
</div>

<!-- Add enough content to ensure scrollbar appears -->
<div style="height: 2000px; background: linear-gradient(to bottom, #f0f0f0, #e0e0e0);">
<p style="padding: 2rem;">Long content to ensure scrollbar is visible...</p>
<p style="padding: 2rem;">Notice that when the modal opens, there's no layout shift!</p>
</div>

<search-modal></search-modal>
</body>
<script>
Expand Down
8 changes: 8 additions & 0 deletions packages/search-widget/src/search-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ export class SearchModal extends LitElement {
}

if (this.open) {
// Calculate scrollbar width to prevent layout shift
const scrollbarWidth =
window.innerWidth - document.documentElement.clientWidth;
document.body.style.overflow = 'hidden';
// Add padding to compensate for scrollbar removal
if (scrollbarWidth > 0) {
document.body.style.paddingRight = `${scrollbarWidth}px`;
}
} else {
document.body.style.removeProperty('overflow');
document.body.style.removeProperty('padding-right');
}
}

Expand Down