PPolaris ComponentsConnect an agent

EmptyState

Fills a page or card that has no data yet with an illustration, an explanation and the action that creates the first record.

Preview

Code

Example.jsx
import { useState } from 'react';
import { Card, EmptyState, BlockStack, InlineStack, Text, Badge, Button } from '@shopify/polaris';

const EMPTY_STATE_IMAGE = 'https://cdn.shopify.com/s/files/1/0757/9955/files/empty-state.svg';

const DISCOUNTS = [
  { id: 'summer', title: 'Summer sale', detail: '20% off outerwear', tone: 'success', status: 'Active' },
  { id: 'welcome', title: 'Welcome offer', detail: '$10 off first order', tone: 'info', status: 'Draft' },
];

export const Example = () => {
  const [discounts, setDiscounts] = useState([]);

  if (!discounts.length) {
    return (
      <Card>
        <EmptyState
          heading="Create your first discount"
          image={EMPTY_STATE_IMAGE}
          action={{ content: 'Create discount', onAction: () => setDiscounts(DISCOUNTS) }}
          secondaryAction={{ content: 'Learn about discounts', onAction: () => {} }}
        >
          <p>Discounts let you offer a percentage, a fixed amount, or free shipping to the customers you choose.</p>
        </EmptyState>
      </Card>
    );
  }

  return (
    <Card>
      <BlockStack gap="300">
        <InlineStack align="space-between" blockAlign="center">
          <Text as="h2" variant="headingMd">
            Discounts
          </Text>
          <Button onClick={() => setDiscounts([])}>Reset to empty state</Button>
        </InlineStack>
        {discounts.map((discount) => (
          <InlineStack key={discount.id} align="space-between" blockAlign="center">
            <BlockStack gap="050">
              <Text as="span" fontWeight="medium">
                {discount.title}
              </Text>
              <Text as="span" variant="bodySm" tone="subdued">
                {discount.detail}
              </Text>
            </BlockStack>
            <Badge tone={discount.tone}>{discount.status}</Badge>
          </InlineStack>
        ))}
      </BlockStack>
    </Card>
  );
};