PPolaris ComponentsConnect an agent

FAQ

A frequently asked questions accordion with a contact footer, built with Polaris Card, Collapsible, UnstyledButton and Banner.

Preview

Code

Example.jsx
import { useState } from 'react';
import { Banner, BlockStack, Box, Button, Card, Collapsible, Icon, InlineStack, List, Text, UnstyledButton } from '@shopify/polaris';
import { MinusIcon, PlusIcon } from '@shopify/polaris-icons';

const FAQS = [
  {
    id: 'install',
    question: 'The widget is not showing on my storefront',
    answer:
      'Open Online Store → Themes → Customize, then make sure the app embed is toggled on for your live theme. Changes go live as soon as you save.',
  },
  {
    id: 'billing',
    question: 'How does billing work?',
    answer:
      'Charges are handled by Shopify and appear on your regular Shopify invoice. Switching plans is prorated, so you only pay the difference.',
    options: ['Change or cancel your plan at any time', 'No charge while you are on the free plan'],
  },
  {
    id: 'support',
    question: 'What are your support hours?',
    answer:
      'Live chat is staffed 24/7 on business days and we reply to email within a few hours. Priority support is included on the Advanced plan.',
  },
];

const COLLAPSIBLE_TRANSITION = { duration: '200ms', timingFunction: 'ease-in-out' };
const QUESTION_BUTTON_STYLE = { display: 'block', width: '100%', cursor: 'pointer', textAlign: 'left' };

const FaqRow = ({ id, question, answer, options, isOpen, onToggle }) => {
  const panelId = `faq-${id}`;

  return (
    <Box
      borderBlockStartWidth="025"
      borderColor="border-secondary"
      paddingInline="400"
      paddingBlock="300"
      background={isOpen ? 'bg-surface-secondary' : 'bg-surface'}
    >
      <BlockStack>
        <UnstyledButton
          style={QUESTION_BUTTON_STYLE}
          ariaExpanded={isOpen}
          ariaControls={panelId}
          accessibilityLabel={isOpen ? `Collapse ${question}` : `Expand ${question}`}
          onClick={onToggle}
        >
          <InlineStack align="space-between" blockAlign="center" wrap={false}>
            <Text as="span" fontWeight="medium">
              {question}
            </Text>
            <InlineStack>
              <Icon source={isOpen ? MinusIcon : PlusIcon} tone="base" />
            </InlineStack>
          </InlineStack>
        </UnstyledButton>
        <Collapsible open={isOpen} id={panelId} transition={COLLAPSIBLE_TRANSITION}>
          <Box paddingBlockStart="200">
            <BlockStack gap="200">
              <Text as="p" tone="subdued">
                {answer}
              </Text>
              {options && (
                <List gap="extraTight" type="bullet">
                  {options.map((option) => (
                    <List.Item key={option}>{option}</List.Item>
                  ))}
                </List>
              )}
            </BlockStack>
          </Box>
        </Collapsible>
      </BlockStack>
    </Box>
  );
};

export const Example = () => {
  const [contactRequested, setContactRequested] = useState(false);
  const [openFaqId, setOpenFaqId] = useState('install');

  const toggleFaq = (id) => setOpenFaqId((prev) => (prev === id ? null : id));

  return (
    <BlockStack gap="400">
      <Card padding="0">
        <Box padding="400">
          <Text as="h3" variant="headingSm">
            Frequently asked questions
          </Text>
        </Box>
        {FAQS.map((faq) => (
          <FaqRow
            key={faq.id}
            {...faq}
            isOpen={openFaqId === faq.id}
            onToggle={() => toggleFaq(faq.id)}
          />
        ))}
        <Box
          borderBlockStartWidth="025"
          borderColor="border-secondary"
          paddingInline="400"
          paddingBlock="300"
        >
          <InlineStack gap="100" blockAlign="center">
            <Text as="span" tone="subdued">
              Still need help?
            </Text>
            <Button variant="plain" onClick={() => setContactRequested(true)}>
              Contact us
            </Button>
          </InlineStack>
        </Box>
      </Card>
      {contactRequested && (
        <Banner
          tone="success"
          title="We got your message"
          onDismiss={() => setContactRequested(false)}
        >
          <Text as="p">A support agent will reply in the chat widget shortly.</Text>
        </Banner>
      )}
    </BlockStack>
  );
};