PPolaris ComponentsConnect an agent

Text

The typography primitive for every string in a Polaris app, with heading and body variants, semantic element choice, tone, weight, alignment, truncation and numeric figures.

Preview

Code

Example.jsx
import { Card, Box, BlockStack, InlineStack, InlineGrid, Text, Divider } from '@shopify/polaris';

const HEADING_VARIANTS = [
  { variant: 'heading3xl', as: 'h1', label: 'heading3xl' },
  { variant: 'heading2xl', as: 'h1', label: 'heading2xl' },
  { variant: 'headingXl', as: 'h2', label: 'headingXl' },
  { variant: 'headingLg', as: 'h3', label: 'headingLg' },
  { variant: 'headingMd', as: 'h4', label: 'headingMd' },
  { variant: 'headingSm', as: 'h5', label: 'headingSm' },
  { variant: 'headingXs', as: 'h6', label: 'headingXs' },
];

const BODY_VARIANTS = [
  { variant: 'bodyLg', label: 'bodyLg' },
  { variant: 'bodyMd', label: 'bodyMd' },
  { variant: 'bodySm', label: 'bodySm' },
  { variant: 'bodyXs', label: 'bodyXs' },
];

const TONES = [
  { tone: 'base', description: 'Default body copy' },
  { tone: 'subdued', description: 'Secondary and helper copy' },
  { tone: 'critical', description: 'Payment failed' },
  { tone: 'success', description: 'Order fulfilled' },
  { tone: 'caution', description: 'Low inventory' },
];

const WEIGHTS = ['regular', 'medium', 'semibold', 'bold'];

const ALIGNMENTS = ['start', 'center', 'end'];

const ELEMENTS = [
  { as: 'h2', note: 'Section heading in the document outline' },
  { as: 'p', note: 'A paragraph of running copy' },
  { as: 'span', note: 'Inline text inside another sentence' },
  { as: 'strong', note: 'Text with strong importance' },
];

const SPEC_COLUMNS = { xs: '110px 1fr' };

const SpecRow = ({ name, children }) => (
  <InlineGrid columns={SPEC_COLUMNS} gap="400" alignItems="center">
    <Text as="span" variant="bodySm" tone="subdued" numeric>
      {name}
    </Text>
    {children}
  </InlineGrid>
);

const Section = ({ title, children }) => (
  <BlockStack gap="300">
    <Text as="h2" variant="headingSm" tone="subdued">
      {title}
    </Text>
    {children}
  </BlockStack>
);

export const Example = () => (
  <BlockStack gap="400">
    <Card>
      <BlockStack gap="500">
        <Section title="Heading variants">
          <BlockStack gap="300">
            {HEADING_VARIANTS.map((item) => (
              <SpecRow key={item.variant} name={item.label}>
                <Text as={item.as} variant={item.variant}>
                  Abandoned checkouts
                </Text>
              </SpecRow>
            ))}
          </BlockStack>
        </Section>

        <Divider />

        <Section title="Body variants">
          <BlockStack gap="300">
            {BODY_VARIANTS.map((item) => (
              <SpecRow key={item.variant} name={item.label}>
                <Text as="p" variant={item.variant}>
                  Customers who left items in their cart in the last 30 days.
                </Text>
              </SpecRow>
            ))}
          </BlockStack>
        </Section>
      </BlockStack>
    </Card>

    <Card>
      <Section title="Tone">
        <BlockStack gap="200">
          {TONES.map((item) => (
            <SpecRow key={item.tone} name={item.tone}>
              <Text as="p" tone={item.tone}>
                {item.description}
              </Text>
            </SpecRow>
          ))}
        </BlockStack>
      </Section>
    </Card>

    <Card>
      <Section title="Font weight">
        <BlockStack gap="200">
          {WEIGHTS.map((weight) => (
            <SpecRow key={weight} name={weight}>
              <Text as="p" variant="bodyLg" fontWeight={weight}>
                Sunrise ceramic mug
              </Text>
            </SpecRow>
          ))}
        </BlockStack>
      </Section>
    </Card>

    <Card>
      <BlockStack gap="500">
        <Section title="The as prop sets the rendered element">
          <BlockStack gap="200">
            {ELEMENTS.map((item) => (
              <SpecRow key={item.as} name={`as="${item.as}"`}>
                <Text as={item.as}>{item.note}</Text>
              </SpecRow>
            ))}
          </BlockStack>
        </Section>

        <Divider />

        <Section title="Alignment">
          <BlockStack gap="200">
            {ALIGNMENTS.map((alignment) => (
              <Box key={alignment} background="bg-surface-secondary" padding="200" borderRadius="200">
                <Text as="p" alignment={alignment}>
                  {alignment}
                </Text>
              </Box>
            ))}
          </BlockStack>
        </Section>
      </BlockStack>
    </Card>

    <Card>
      <BlockStack gap="500">
        <Section title="Truncate">
          <Box width="260px">
            <Text as="p" truncate>
              Midnight blue cotton crewneck sweater with ribbed cuffs, size medium
            </Text>
          </Box>
        </Section>

        <Divider />

        <Section title="Numeric aligns figures in tables">
          <BlockStack gap="100">
            <InlineStack align="space-between">
              <Text as="span" tone="subdued">
                Subtotal
              </Text>
              <Text as="span" numeric>
                $1,148.00
              </Text>
            </InlineStack>
            <InlineStack align="space-between">
              <Text as="span" tone="subdued">
                Shipping
              </Text>
              <Text as="span" numeric>
                $12.50
              </Text>
            </InlineStack>
            <InlineStack align="space-between">
              <Text as="span" fontWeight="semibold">
                Total
              </Text>
              <Text as="span" fontWeight="semibold" numeric>
                $1,160.50
              </Text>
            </InlineStack>
          </BlockStack>
        </Section>
      </BlockStack>
    </Card>
  </BlockStack>
);