-
Notifications
You must be signed in to change notification settings - Fork 23
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
docs(example template): amending example template #979
Open
murrlipp
wants to merge
7
commits into
main
Choose a base branch
from
update-example-box
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−28
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b71718
docs(example template): amending example template
murrlipp 1370d50
docs(examples): fix margin and padding around example tabs
chrispymm e9bc9ae
docs(examples): add dynamic resizing to ensure iframe is always the c…
chrispymm 41f88c6
docs(examples): adjust scope of the mutationObserver to ensure it tri…
chrispymm a3ebfe2
Merge branch 'main' into update-example-box
murrlipp 8dfb0e6
Merge branch 'main' into update-example-box
murrlipp d1090e6
docs(example template): remove commented out line
murrlipp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
export default class IFrameResizer { | ||
constructor(iframe) { | ||
this.iframe = iframe; | ||
this.observer = null; | ||
this.contentWindow = null; | ||
|
||
// Bind methods | ||
this.init = this.init.bind(this); | ||
this.cleanup = this.cleanup.bind(this); | ||
this.onLoad = this.onLoad.bind(this); | ||
this.onResize = this.onResize.bind(this); | ||
this.onMutation = this.onMutation.bind(this); | ||
|
||
// Start initialization | ||
this.iframe.addEventListener("load", this.onLoad); | ||
} | ||
|
||
init() { | ||
try { | ||
this.contentWindow = this.iframe.contentWindow; | ||
|
||
// Create ResizeObserver to watch the iframe content | ||
this.resizeObserver = new ResizeObserver((entries) => { | ||
for (const entry of entries) { | ||
this.onResize(entry); | ||
} | ||
}); | ||
|
||
// Create MutationObserver to watch for visibility changes | ||
this.mutationObserver = new MutationObserver((mutations) => { | ||
for (const mutation of mutations) { | ||
this.onMutation(mutation); | ||
} | ||
}); | ||
|
||
// Observe the body of the iframe content | ||
const targetNode = this.contentWindow.document.body; | ||
this.resizeObserver.observe(targetNode); | ||
|
||
// Observe for attribute changes that might affect visibility | ||
this.mutationObserver.observe(targetNode, { | ||
attributes: true, | ||
attributeFilter: ["style", "class", "hidden", "aria-expanded"], | ||
attributeOldValue: true, | ||
childList: true, | ||
subtree: true, | ||
}); | ||
|
||
// Initial size adjustment | ||
this.adjustSize(); | ||
} catch (error) { | ||
console.error("Failed to initialize IframeResizer:", error); | ||
} | ||
} | ||
|
||
onLoad() { | ||
this.init(); | ||
} | ||
|
||
onMutation(mutation) { | ||
// Ideally we might want to restrict this slightly to check if we | ||
// need to adjust size, but this is tricky. Most of our components are | ||
// relatively static, so if something changes its likely to be | ||
// visibility-related | ||
this.adjustSize(); | ||
} | ||
|
||
onResize(entry) { | ||
this.adjustSize(); | ||
} | ||
|
||
adjustSize() { | ||
if (!this.contentWindow) return; | ||
|
||
try { | ||
const body = this.contentWindow.document.body; | ||
const html = this.contentWindow.document.documentElement; | ||
const elements = body.getElementsByTagName("*"); | ||
|
||
let maxHeight = html.offsetHeight; | ||
let padding = 30; | ||
|
||
// Check each element's bottom edge position | ||
for (const element of elements) { | ||
const rect = element.getBoundingClientRect(); | ||
const bottomPos = rect.top + rect.height; | ||
// If maxHeight is bigger, that includes the body padding, if bottomPos | ||
// is higher, that is the exact bottom of the element, so we add some padding | ||
maxHeight = maxHeight > bottomPos ? maxHeight : bottomPos + padding; | ||
} | ||
|
||
// Update iframe height | ||
this.iframe.style.height = `${maxHeight}px`; | ||
} catch (error) { | ||
console.error("Failed to adjust iframe size:", error); | ||
} | ||
} | ||
|
||
cleanup() { | ||
if (this.resizeObserver) { | ||
this.resizeObserver.disconnect(); | ||
} | ||
if (this.mutationObserver) { | ||
this.mutationObserver.disconnect(); | ||
} | ||
this.iframe.removeEventListener("load", this.onLoad); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
sorry, just spotted a space after the govuk-details class name. It is my last comment though!