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

docs(example template): amending example template #979

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
17 changes: 10 additions & 7 deletions docs/_includes/example.njk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="app-example">
<span class="app-example__new-window">
<div class="app-example__new-window">
<a href="{{ href | url }}" target="_blank">Open this example <span class="govuk-visually-hidden">({{ title }})</span> in a new window</a>
</span>
</div>
<div class="app-example">
<iframe class="app-example__frame" scrolling="auto" frameborder="0" height="{{ height }}" src="{{ href | url }}" title="{{ title }}"></iframe>
</div>
<div class="app-tabs" data-module="app-tabs">
Expand All @@ -24,17 +24,20 @@
{%- endif -%}
</ul>

<div class="app-tabs__panel app-tabs__panel--hidden" id="html-default-{{ id }}" data-module="app-copy" role="tabpanel" aria-labelledby="tab_html-default-{{ id }}">
<div class="app-tabs__panel app-tabs__panel--hidden" id="html-default-{{ id }}" role="tabpanel" aria-labelledby="tab_html-default-{{ id }}">

<div data-module="app-copy" style="position:relative;">

```html
{{ htmlCode | safe }}
```
</div>
</div>

<div class="app-tabs__panel app-tabs__panel--hidden" id="nunjucks-default-{{ id }}" role="tabpanel" aria-labelledby="tab_nunjucks-default-{{ id }}">

{% if arguments %}
<details class="govuk-details govuk-!-padding-3" data-module="govuk-details">
<details class="govuk-details " data-module="govuk-details">
<summary class="govuk-details__summary">

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!

<span class="govuk-details__summary-text">
Nunjucks macro options
Expand Down Expand Up @@ -68,13 +71,13 @@
{% endif %}

{% if figmaLink %}
<div class="app-tabs__panel govuk-!-padding-3 app-tabs__panel--hidden" id="figma-default-{{ id }}" role="tabpanel" aria-labelledby="tab_figma-default-{{ id }}">
<div class="app-tabs__panel app-tabs__panel--hidden" id="figma-default-{{ id }}" role="tabpanel" aria-labelledby="tab_figma-default-{{ id }}">

This component is in the ‘Assets’ tab in the MoJ Figma Kit.

If you’re external to MoJ, download the Kit and add it as a library to your Figma files. You’ll need to re-add the kit to update the version.

[View component in MoJ Figma Kit]({{ figmaLink }})
<p class="govuk-body govuk-!-margin-0"><a href="{{ figmaLink }}">View component in MoJ Figma Kit</a></p>

</div>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/layouts/example.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<style>{% include (page.inputPath | getStylesPath) ignore missing %}</style>
{% endblock %}
{% block body %}
<div class="app-example__wrapper govuk-!-margin-6 govuk-!-margin-top-9">{{ content | safe }}</div>
<div class="app-example__wrapper govuk-!-margin-6">{{ content | safe }}</div>
{% endblock %}
{% block pageScripts %}
<script type="module">{% include (page.inputPath | getScriptPath) ignore missing %}</script>
Expand Down
4 changes: 4 additions & 0 deletions docs/assets/javascript/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MOJFrontend from "../../../package/moj/all.js";
import Cookies from "./cookies";
import Copy from "./copy";
import Tabs from "./tabs";
import IFrameResizer from './iframe-resizer.js'

import MenuToggle from "./menu-toggle.js";
import CollapsibleNav from "./collapsible-nav.js";
Expand All @@ -23,6 +24,9 @@ $(function () {
$('[data-module="app-cookies"]').each(function (e, el) {
new Cookies(el).init();
});

const iFrames = document.querySelectorAll('iframe')
iFrames.forEach((frame) => new IFrameResizer(frame))
});

window.MOJFrontend = MOJFrontend;
Expand Down
108 changes: 108 additions & 0 deletions docs/assets/javascript/iframe-resizer.js
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);
}
}
4 changes: 2 additions & 2 deletions docs/assets/stylesheets/components/_copy-button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
@include govuk-font(16);
position: absolute;
z-index: 1;
top: govuk-spacing(4);
right: govuk-spacing(4);
top: govuk-spacing(3);
right: govuk-spacing(3);
min-width: 110px;
padding: 4px 10px 2px 10px;
border: 1px solid $copy-button-colour;
Expand Down
12 changes: 5 additions & 7 deletions docs/assets/stylesheets/components/_example.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

.app-example {
background-color: govuk-colour("white");
border: 1px solid $govuk-border-colour;
border-left: 1px solid $govuk-border-colour;
border-right: 1px solid $govuk-border-colour;
position: relative;
}


.app-example__frame {
background-color: govuk-colour("white");
border: 0;
Expand All @@ -26,16 +26,14 @@
.app-example__new-window {
@include govuk-font($size: 16);
border: 1px solid $govuk-border-colour;
position: absolute; top: -1px; left: -1px;
background-color: white;

background-color: govuk-colour("white");
padding: 8px;

a,
a:link,
a:visited {
color: govuk-colour("blue");
display: block;
margin: 8px;
// display: block;
murrlipp marked this conversation as resolved.
Show resolved Hide resolved
text-decoration: none;
}

Expand Down
6 changes: 3 additions & 3 deletions docs/assets/stylesheets/components/_prose.scss
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
}

pre {
@include govuk-responsive-margin(4, "bottom");
background: #f8f8f8; // Matches _higlight.scss
@include govuk-responsive-margin(6, "bottom");
background: #f7f7f7;
font-size: 16px;
margin: 0;
margin: govuk-spacing(6);
padding: govuk-spacing(3);
position: relative;
overflow: auto;
Expand Down
14 changes: 6 additions & 8 deletions docs/assets/stylesheets/components/_tabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

.app-tabs {
background: govuk-colour("white");
margin-bottom: govuk-spacing(6);
border-top: 0;
margin-bottom: govuk-spacing(9);
}

.app-tabs__list {
border: 1px solid $govuk-border-colour;
border-top: 0;
list-style: none;
margin: 0;
padding: 0;
Expand Down Expand Up @@ -72,7 +72,7 @@
}

&[aria-selected="true"] {
background-color: govuk-colour("light-grey");
background-color: govuk-colour("white");
color: govuk-colour("black");
margin-bottom: -1px;
padding-bottom: 21px;
Expand All @@ -86,24 +86,22 @@

.app-tabs__panel {
@include govuk-font($size: 16);
background-color: govuk-colour("light-grey");
background-color: govuk-colour("white");
border: 1px solid $govuk-border-colour;
border-top: 0;
position: relative;
padding: govuk-spacing(3);

&--hidden {
display: none;
}

*:last-of-type {
margin-bottom: 0; // Remove margin bottom from any last element
}

p {
max-width: 100% !important;
}

pre {
margin: 0;
padding-top: govuk-spacing(9);
}
}
Loading