FAQ Accordion using Skeleton UI

This commit is contained in:
Antoine Phan
2025-06-14 01:08:49 -04:00
parent f3285c21fd
commit 469bc6ba85
3 changed files with 92 additions and 39 deletions

View File

@@ -1,12 +1,68 @@
<script> <script>
import { slide } from 'svelte/transition'; // import { slide } from 'svelte/transition';
let isOpen = false; import { Accordion } from '@skeletonlabs/skeleton-svelte';
export let entry;
const { title, description } = entry; // FAQ closed by default
const toggle = () => (isOpen = !isOpen); let isOpen = $state(false);
// const toggle = () => (isOpen = !isOpen);
// Variable to be included into the component
let { /** @type {String} */ entries } = $props();
</script> </script>
<div class="accordion-wrapper w-[500px]"> <Accordion classes="w-[500px] max-w-[500px] transition-all ease-in-out" multiple>
{#snippet iconClosed()}
<svg
width="20"
height="20"
fill="none"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
stroke="currentColor"
><path d="M9 5l7 7-7 7" />
</svg>
{/snippet}
{#snippet iconOpen()}
<svg
class="rotate-90"
width="20"
height="20"
fill="none"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
stroke="currentColor"
><path d="M9 5l7 7-7 7" />
</svg>
{/snippet}
{#each entries as entry, index}
<Accordion.Item
value={index.toString()}
classes="border-ecsess-200 mb-4 overflow-hidden rounded-xl border-2"
leadClasses="text-xl font-bold"
controlClasses="hover:bg-ecsess-black-hover
flex w-inherit cursor-pointer items-center justify-between
py-4 transition-colors ease-in-out"
panelClasses=" border-t-ecsess-200 max-h-fit border-t-2 bg-transparent p-4 bg-ecsess-400"
>
<!-- Control -->
{#snippet lead()}
{entry.question}
{/snippet}
{#snippet control()}{/snippet}
<!-- Panel -->
{#snippet panel()}{entry.answer}{/snippet}
</Accordion.Item>
{/each}
</Accordion>
<!--
<div class="accordion-wrapper w-full max-w-[500px]">
<div <div
class="accordion-item class="accordion-item
border-ecsess-200 mb-4 overflow-hidden rounded-xl border-2 border-ecsess-200 mb-4 overflow-hidden rounded-xl border-2
@@ -16,12 +72,12 @@
class="accordion-header hover:bg-ecsess-black-hover class="accordion-header hover:bg-ecsess-black-hover
flex h-16 w-full cursor-pointer items-center justify-between flex h-16 w-full cursor-pointer items-center justify-between
px-4 transition-colors ease-in-out" px-4 transition-colors ease-in-out"
on:click={toggle} onclick={toggle}
aria-expanded={isOpen} aria-expanded={isOpen}
> >
<span class="font-bold text-xl">{title}</span> <span class="text-xl font-bold">{question}</span>
<svg <svg
class="accordion-icon transform-cpu ease-in-out transition-all" class="accordion-icon transform-cpu transition-all ease-in-out"
width="20" width="20"
height="20" height="20"
fill="none" fill="none"
@@ -36,14 +92,17 @@
</button> </button>
{#if isOpen} {#if isOpen}
<div class="accordion-content max-h-fit border-t-2 border-t-ecsess-200 p-4 bg-transparent transition-all ease-in" transition:slide={{ duration: 300 }}> <div
<p>{@html description}</p> class="accordion-content border-t-ecsess-200 max-h-fit border-t-2 bg-transparent p-4 transition-all ease-in"
transition:slide={{ duration: 300 }}
>
<p>{answer}</p>
</div> </div>
{/if} {/if}
</div> </div>
</div> </div> -->
<style> <!-- <style>
[aria-expanded='true'] .accordion-icon { [aria-expanded='true'] .accordion-icon {
transform: rotate(90deg); transform: rotate(90deg);
} }
@@ -52,4 +111,4 @@
.accordion-item.open .accordion-content { .accordion-item.open .accordion-content {
background-color: var(--color-ecsess-black-hover); background-color: var(--color-ecsess-black-hover);
} }
</style> </style> -->

View File

@@ -2,7 +2,8 @@ import { getFromCMS } from 'utils/utils.js';
const homepageQuery = `*[_type == "homepage"]{ const homepageQuery = `*[_type == "homepage"]{
"description": description[], "description": description[],
"councilPhoto": councilPhoto.asset->url "councilPhoto": councilPhoto.asset->url,
"faqs": faqs[]{ question, answer },
}[0]`; }[0]`;
const ohQuery = `*[_type=="oh"].schedule[]{ const ohQuery = `*[_type=="oh"].schedule[]{
@@ -13,12 +14,26 @@ const ohQuery = `*[_type=="oh"].schedule[]{
}`; }`;
export const load = async () => { export const load = async () => {
/**
* @description Response data type based on the `homepageQuery` above.
* Note that `description` is a rich/portable text type
*
* @type {{
* description: import('@portabletext/svelte').InputValue,
* councilPhoto: string,
* faqs: [{
* question: string,
* answer: string
* }],
* }}
*
*/
let CMSresponse = await getFromCMS(homepageQuery); let CMSresponse = await getFromCMS(homepageQuery);
return { return {
description: CMSresponse.description, description: CMSresponse.description,
councilPhoto: CMSresponse.councilPhoto councilPhoto: CMSresponse.councilPhoto,
faqs: CMSresponse.faqs
// ohs: await getFromCMS(ohQuery), // ohs: await getFromCMS(ohQuery),
// FAQs: "",
}; };
}; };

View File

@@ -5,25 +5,6 @@
/** loading things from the server side */ /** loading things from the server side */
let { data } = $props(); let { data } = $props();
// Getting info from ECSESS CMS
let longAnswer =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
const faqs = [
{
q: 'A fequently asked question?',
a: 'This is a short answer'
},
{
q: 'A fequently asked question?',
a: 'This is a medium length answer. Lor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
},
{
q: 'A fequently asked question?',
a: 'This is a long answer. ' + longAnswer
}
];
</script> </script>
<title> McGill ECSESS </title> <title> McGill ECSESS </title>
@@ -46,9 +27,7 @@
</div> </div>
<div> <div>
<h1>FAQ</h1> <h1>FAQ</h1>
{#each faqs as { q, a }} <FaqAccordion entries={data.faqs} />
<FaqAccordion entry={{ title: q, description: a }} />
{/each}
</div> </div>
</Section> </Section>
<!-- Office Hours Calendar --> <!-- Office Hours Calendar -->