HPE
Design System
Foundation
Components
Templates
Learn
Jump to section
Guidance
Theming
Scrolling
Line wrapping
Tab index
Templates

Code Blocks

A standardized style for displaying code.

import React from 'react';
import PropTypes from 'prop-types';
import { Box, Card, CardBody, CardFooter, Chart, Text, Stack } from 'grommet';
import { Wifi, StatusWarningSmall } from 'grommet-icons';

const mockData = Array(30)
  .fill()
  .map((_, index) => ({
    value: [index, Math.random() * 100],
  }));

const capacityWarnings = mockData.filter(datum => datum.value[1] >= 50).length;

const gradient = [
  { value: 0, color: 'status-ok' },
  { value: 33, color: 'status-ok' },
  { value: 67, color: 'status-warning' },
  { value: 85, color: 'status-critical' },
];

export const CardExample = () => (
  <Card
    background="background-front"
    onClick={() => {
      // eslint-disable-next-line no-alert
    }}
    width="medium"
  >
    <CardBody pad="none">
      <Identifier
        title="Network Traffic"
        subtitle="Bandwidth Utilization - Last 30 Days"
        icon={<Wifi size="large" color="text-strong" />}
        level={2}
      />
      <Box margin={{ top: 'medium' }}>
        <KPIChart id="metric-0" data={mockData} />
      </Box>
    </CardBody>
    <CardFooter background="background-back">
      <KPISummary instances={capacityWarnings} statusColor="status-warning" />
    </CardFooter>
  </Card>
);

const Identifier = ({ title, subtitle, icon }) => (
  <Box
    direction="row"
    gap="medium"
    align="center"
    pad={{ horizontal: 'medium', top: 'medium' }}
  >
    <Box pad={{ vertical: 'xsmall' }}>{icon}</Box>
    <Box>
      <Text color="text-strong" size="xxlarge" weight="bold">
        {title}
      </Text>
      <Text>{subtitle}</Text>
    </Box>
  </Box>
);

Identifier.propTypes = {
  title: PropTypes.node,
  subtitle: PropTypes.node,
  icon: PropTypes.node,
};

const KPIChart = ({ data, ...rest }) => (
  <Stack>
    <Chart
      type="area"
      thickness="xxsmall"
      values={data}
      color="background-back"
      size={{ height: 'xsmall' }}
      {...rest}
    />
    <Chart
      a11yTitle="Card displaying network traffic"
      type="line"
      thickness="xxsmall"
      values={data}
      color={gradient}
      size={{ height: 'xsmall' }}
      {...rest}
    />
  </Stack>
);

KPIChart.propTypes = {
  data: PropTypes.arrayOf(
    PropTypes.shape({
      value: PropTypes.array,
    }),
  ),
};

const KPISummary = ({ instances, statusColor }) => (
  <Box direction="row" align="center" gap="small">
    <StatusWarningSmall color={statusColor} size="small" />
    <Text>{instances} instances above utilization target</Text>
  </Box>
);

KPISummary.propTypes = {
  instances: PropTypes.number,
  statusColor: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.shape({
      dark: PropTypes.string,
      light: PropTypes.string,
    }),
  ]),
};

Guidance

Code blocks can be created using the react-syntax-highlighter tool.

Prism or Highlighter.js can be used with react-syntax-highlighter. Highlighter.js is the default, however it can have trouble highlighting JSX, so it is recommended to use Prism instead. Prism with react-syntax-highlighter supports syntax highlighting for hundreds of languages.

react-syntax-highlighter can be installed with the following command:

For yarn users: yarn add react-syntax-highlighter

For npm users: npm install react-syntax-highlighter --save

Import the react-syntax-highlighter specifying Prism as the highlighter with the following:

import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';

Theming

The theme exported from grommet-hpe-theme should be used to style code blocks.

Scrolling

Horizontal scrolling should be avoided where possible.

Vertical scrolling within the code block container is encouraged when a code block is long.

Line wrapping

Long lines should wrap to avoid horizontal scrolling. This option can be specified with wrapLongLines.

Tab index

To ensure the code block is accessible to keyboard users the tabIndex should be set to 0.

Still have questions?

Something missing or looking for more information? Get in touch to help make the HPE Design System better.

Edit this page