-
Notifications
You must be signed in to change notification settings - Fork 4
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
Makes the code example an exported component #264
Draft
frankieroberto
wants to merge
1
commit into
main
Choose a base branch
from
add-example-as-component
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.
Draft
Changes from all commits
Commits
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,72 @@ | ||
--- | ||
layout: sub-navigation | ||
order: 3 | ||
title: Code example | ||
description: Show users how to use your components | ||
--- | ||
{% from "example/macro.njk" import xGovukExample %} | ||
|
||
{{ xGovukExample({ | ||
idPrefix: "code-example", | ||
previewPath: "/examples/code-example", | ||
codeSamples: [ | ||
{ | ||
label: { | ||
text: "HTML" | ||
}, | ||
content: { | ||
html: getHtmlCode("code-example") | ||
} | ||
}, | ||
{ | ||
label: { | ||
text: "Nunjucks" | ||
}, | ||
content: { | ||
html: getNunjucksCode("code-example") | ||
} | ||
} | ||
] | ||
}) }} | ||
|
||
This component is used in documentation websites (like this one) to show examples and the code behind them. | ||
|
||
|
||
## Nunjucks macro options | ||
|
||
{% from "govuk/components/table/macro.njk" import govukTable %} | ||
{{ govukTable({ | ||
firstCellIsHeader: true, | ||
head: [ | ||
{ text: "Name" }, | ||
{ text: "Type" }, | ||
{ text: "Description" } | ||
], | ||
rows: [ | ||
[ | ||
{ text: "idPrefix" }, | ||
{ text: "string" }, | ||
{ text: "**Required**. A unique ID for the component on the page" } | ||
], | ||
[ | ||
{ text: "previewPath" }, | ||
{ text: "string" }, | ||
{ text: "URL path to a page where the example can be viewed" } | ||
], | ||
[ | ||
{ text: "codeSamples" }, | ||
{ text: "array" }, | ||
{ text: "A list of code samples in different languages" } | ||
], | ||
[ | ||
{ text: "codeSamples.label.text" }, | ||
{ text: "string" }, | ||
{ text: "The text label for the code sample, for example 'HTML'" } | ||
], | ||
[ | ||
{ text: "codeSamples.content.html" }, | ||
{ text: "string" }, | ||
{ text: "The code used in that sample. You may need to escape this, and may wany to apply code formatting styles." } | ||
] | ||
] | ||
}) }} |
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,6 @@ | ||
--- | ||
eleventyExcludeFromCollections: true | ||
layout: example-full-width.njk | ||
title: Code example | ||
--- | ||
<p>This is an <strong>example</strong>.</p> |
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,37 @@ | ||
--- | ||
eleventyExcludeFromCollections: true | ||
layout: example-full-width.njk | ||
title: Code example | ||
--- | ||
{% from "x-govuk/components/example/macro.njk" import xGovukExample %} | ||
|
||
{% set htmlCode %} | ||
<p>This is an <strong>example </strong>. </p> | ||
{% endset %} | ||
{% set nunjucksCode %} | ||
{% raw %}{{ paragraph({ html: "This is an <strong>example </strong>." }) }}{% endraw %} | ||
{% endset %} | ||
|
||
|
||
{{ xGovukExample({ | ||
idPrefix: "my-code", | ||
previewPath: "/examples/code-example-output", | ||
codeSamples: [ | ||
{ | ||
label: { | ||
text: "HTML" | ||
}, | ||
content: { | ||
html: htmlCode | ||
} | ||
}, | ||
{ | ||
label: { | ||
text: "Nunjucks" | ||
}, | ||
content: { | ||
html: nunjucksCode | ||
} | ||
} | ||
] | ||
}) }} |
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.
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.
The requirement to add the iFrame script to both the source content and the page that consumes the component either needs incorporating into the component somehow, else documenting how to set up. Part of me wonders if their might be something cleaver we could do with web components, but that’d be very different to how other components work.
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.
Yeah this is super annoying. Seems odd that there’s no way to set an iframe to natively resize to its contents but I guess there were performance reasons why that wasn’t built in?
Could bundle the iframeresizer script with the
xGovukExample
component javascript itself, but that means an extra dependency and might be tricky? Possibly worth investigating still though. Otherwise we’d just have to document it well as something extra that you have to add to make dynamic iframe sizing work. Possibly there’s some contexts where a fixed height would be ok and you wouldn’t need it, but it does feel quite essential?