Skip to content

Commit

Permalink
Refactor DottedLines to use CSS not SVG (#1622)
Browse files Browse the repository at this point in the history
* refactor(DottedLines): CSS not SVG

* Add changeset

---------

Co-authored-by: James Mockett <[email protected]>
  • Loading branch information
mxdvl and jamesmockett authored Aug 5, 2024
1 parent 32628d3 commit d4e48f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-ducks-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/source-development-kitchen': major
---

Refactor `DottedLines` to render pattern with CSS rather than SVG
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SerializedStyles } from '@emotion/react';
import { breakpoints, neutral } from '@guardian/source/foundations';
import { css, type SerializedStyles } from '@emotion/react';
import { breakpoints, palette } from '@guardian/source/foundations';
import type { LineCount } from './Lines';

const dotRadius = 1;
Expand All @@ -8,56 +8,28 @@ const maxWidth = breakpoints.wide;

export const getHeight = (count: LineCount): number => gridSize * count;

const pattern = css`
max-width: ${maxWidth}px;
background-size: ${gridSize}px ${gridSize}px;
background-position: top center;
background-image: radial-gradient(
currentColor,
currentColor ${dotRadius}px,
transparent ${dotRadius}px
);
`;

export const DottedLines = ({
count = 4,
color = neutral[86],
color = palette.neutral[86],
cssOverrides,
}: {
count?: LineCount;
color?: string;
cssOverrides?: SerializedStyles | SerializedStyles[];
}) => {
const dots = [];
for (let index = 1; index <= count; index++) {
dots.push(
<circle
key={index}
fill={color}
cx={gridSize / 2}
cy={gridSize * (index - 1 / 2)}
r={dotRadius}
/>,
);
}

const height = getHeight(count);
const viewBox = `0 0 ${gridSize} ${height}`;

return (
<svg
xmlns="http://www.w3.org/2000/svg"
width="100%"
height={height}
viewBox={viewBox}
preserveAspectRatio="xMinYMin meet"
css={cssOverrides}
aria-hidden="true"
focusable="false"
>
<defs>
<pattern
id="dotted-pattern"
viewBox={viewBox}
width={gridSize}
height={height}
patternUnits="userSpaceOnUse"
preserveAspectRatio="none"
>
{dots}
</pattern>
</defs>

<rect width={maxWidth} height={height} fill="url(#dotted-pattern)" />
</svg>
);
};
}) => (
<div
style={{ height: `${getHeight(count)}px`, color }}
css={[pattern, cssOverrides]}
/>
);

0 comments on commit d4e48f9

Please sign in to comment.