-
Notifications
You must be signed in to change notification settings - Fork 5
Issue #505: create RandomContent component and example demo on BlogLayout #560
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
Draft
lsr-explore
wants to merge
1
commit into
main
Choose a base branch
from
lsr-explore/gh-505/content-randomizer-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.
+67
−0
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 contains hidden or 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,35 @@ | ||
| --- | ||
| // When rendered on the server this, displays the first child and hides the rest. | ||
| // On page load, the client script picks one to show and hides the rest. | ||
| --- | ||
|
|
||
| <div data-random-content> | ||
| <slot /> | ||
| </div> | ||
|
|
||
| <style is:global> | ||
| /* Pre-JS / no-JS fallback: show only the first child. */ | ||
| /* `is:global` because the children come from a slot (consumer-rendered), | ||
| so they don't get this component's scoped class. */ | ||
| [data-random-content]:not([data-random-ready]) > *:nth-child(n + 2) { | ||
| display: none; | ||
| } | ||
| </style> | ||
|
|
||
| <script> | ||
| function randomize() { | ||
| /* In case there are multiple RandomContent components on the page, we need to randomize each one. */ | ||
| document.querySelectorAll("[data-random-content]").forEach((root) => { | ||
| const items = Array.from(root.children); | ||
| if (items.length > 1) { | ||
| const chosen = Math.floor(Math.random() * items.length); | ||
| items.forEach((item, i) => { | ||
| item.toggleAttribute("hidden", i !== chosen); | ||
| }); | ||
| } | ||
| root.setAttribute("data-random-ready", ""); | ||
| }); | ||
| } | ||
| randomize(); | ||
| document.addEventListener("astro:after-swap", randomize); | ||
| </script> |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ import Branding from "@components/Branding.astro"; | |
| import FlexColumn from "@components/FlexColumn.astro"; | ||
| import ImageHeading from "@components/ImageHeading.astro"; | ||
| import Layout from "./Layout.astro"; | ||
| import RandomContent from "@components/RandomContent.astro"; | ||
| import TeamVignette from "@components/TeamVignette.astro"; | ||
| import ThemedSection from "@components/ThemedSection.astro"; | ||
| --- | ||
|
|
@@ -87,6 +88,37 @@ import ThemedSection from "@components/ThemedSection.astro"; | |
| <strong>Blog</strong> | ||
| </slot> | ||
| </ImageHeading> | ||
| {!isEmpty(blogs) && ( | ||
| <ThemedSection style="tertiary"> | ||
| <h2 class="display-4 border-bottom mb-3">Demo: RandomContent Component</h2> | ||
| <p class="mb-3"> | ||
| A different blog post is featured at random on each page load. Reload to see another. | ||
| </p> | ||
| <RandomContent> | ||
| {blogs.slice(0, 5).map((b) => ( | ||
| <article class="border rounded p-3"> | ||
| <h3><a href={`/blog/${b.id}`}>{b.data.title}</a></h3> | ||
| <p class="mb-0">{b.data.description}</p> | ||
| </article> | ||
| ))} | ||
| </RandomContent> | ||
| </ThemedSection> | ||
| )} | ||
| {!isEmpty(authors) && ( | ||
| <ThemedSection style="tertiary"> | ||
| <h2 class="display-4 border-bottom mb-3">Demo: RandomContent with an Author</h2> | ||
| <p class="mb-3"> | ||
| A different blog author is featured at random on each page load. | ||
| </p> | ||
| <RandomContent> | ||
| {authors.slice(0, 5).map((author) => ( | ||
| <div class="border rounded p-3"> | ||
| <TeamVignette member={author} showLinks={true} /> | ||
| </div> | ||
| ))} | ||
| </RandomContent> | ||
| </ThemedSection> | ||
| )} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to reviewer: Changes to this file - BlogLayout are just to demonstrated the component. They will be removed in the final PR> |
||
| <ThemedSection style="tertiary"> | ||
| <div class="d-flex gap-4"> | ||
| <article class:list={[sidebar ? "d-none d-lg-block" : "col", "col-lg-9"]}> | ||
|
|
||
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.
Note to reviewer: Changes to this file - BlogLayout are just to demonstrated the component. They will be removed in the final PR>