Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

💄 Animate the landing FAQ accordion and keep one item open (#3253)

Luke Vella1d5d41c4a81f
diff --git a/apps/landing/src/app/[locale]/globals.css b/apps/landing/src/app/[locale]/globals.css
index fc9b403d7..d09dffac5 100644
--- a/apps/landing/src/app/[locale]/globals.css
+++ b/apps/landing/src/app/[locale]/globals.css
@@ -82,3 +82,36 @@ h2 {
@apply font-medium;
}
}
+
+/*
+ * FAQ accordion. `interpolate-size` is what makes height:auto animatable;
+ * without it the rules below are inert and <details> toggles instantly.
+ */
+@supports (interpolate-size: allow-keywords) {
+ :root {
+ interpolate-size: allow-keywords;
+ }
+
+ .faq-item::details-content {
+ overflow: hidden;
+ block-size: 0;
+ opacity: 0;
+ transition:
+ block-size 200ms var(--ease-out-expo),
+ opacity 200ms var(--ease-out-expo),
+ content-visibility 200ms allow-discrete;
+ /* content-visibility is discrete; without this the content is hidden
+ for the whole closing transition and only the box animates. */
+ }
+
+ .faq-item[open]::details-content {
+ block-size: auto;
+ opacity: 1;
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .faq-item::details-content {
+ transition-property: opacity, content-visibility;
+ }
+}
diff --git a/apps/landing/src/components/home/faq.tsx b/apps/landing/src/components/home/faq.tsx
index c41d779c8..b5a0a6ca4 100644
--- a/apps/landing/src/components/home/faq.tsx
+++ b/apps/landing/src/components/home/faq.tsx
@@ -1,5 +1,5 @@
import { PlusIcon } from "lucide-react";
-import type * as React from "react";
+import * as React from "react";
import { Trans } from "react-i18next/TransWithoutContext";
import {
SectionContent,
@@ -12,16 +12,22 @@ import { getTranslation } from "@/i18n/server";
export function FaqItem({
question,
+ name,
children,
}: {
question: React.ReactNode;
+ /**
+ * Shared across a group so the browser keeps only one item open. Injected by
+ * `Faq`; the exclusive behaviour is lost if an item is rendered outside one.
+ */
+ name?: string;
children: React.ReactNode;
}) {
return (
- <details className="group">
+ <details className="faq-item group" name={name}>
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 py-5 font-medium text-base text-gray-800 [&::-webkit-details-marker]:hidden">
{question}
- <PlusIcon className="size-4 shrink-0 text-gray-400 transition-transform group-open:rotate-45" />
+ <PlusIcon className="size-4 shrink-0 text-gray-400 transition-transform duration-200 ease-out-expo group-open:rotate-45 motion-reduce:transition-none" />
</summary>
<p className="max-w-prose pb-5 text-gray-500 text-sm leading-relaxed sm:text-base">
{children}
@@ -30,8 +36,23 @@ export function FaqItem({
);
}
-export function Faq({ children }: { children: React.ReactNode }) {
- return <div className="divide-y">{children}</div>;
+export function Faq({
+ name = "faq",
+ children,
+}: {
+ /** Only needs setting if a page renders more than one FAQ group. */
+ name?: string;
+ children: React.ReactNode;
+}) {
+ return (
+ <div className="divide-y">
+ {React.Children.map(children, (child) =>
+ React.isValidElement<{ name?: string }>(child)
+ ? React.cloneElement(child, { name })
+ : child,
+ )}
+ </div>
+ );
}
export async function FaqSection({