Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single interactive grid component #1825

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions libs/@guardian/react-crossword/src/components/Controls.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { css } from '@emotion/react';
import { space } from '@guardian/source/foundations';
import type { ThemeButton } from '@guardian/source/react-components';
import { useCallback } from 'react';
import type { Cell, Progress } from '../@types/crossword';
Expand Down Expand Up @@ -66,7 +68,16 @@ const ClueControls = () => {
}, [cells, checkCell, currentEntryId]);

return (
<>
<div
css={css`
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
gap: ${space[1]}px;
padding: ${space[1]}px 0;
`}
>
{currentEntryId && (
<>
<Button onSuccess={clearEntry} theme={crosswordButtonTheme}>
Expand All @@ -87,7 +98,7 @@ const ClueControls = () => {
<Button onSuccess={clearProgress} theme={crosswordButtonTheme}>
Anagram Helper
</Button>
</>
</div>
);
};

Expand Down Expand Up @@ -129,7 +140,16 @@ const GridControls = () => {
}, [cells, checkCell]);

return (
<>
<div
css={css`
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
gap: ${space[1]}px;
padding: ${space[1]}px 0;
`}
>
{solutionAvailable && (
<>
<Button onSuccess={checkGrid} requireConfirmation={true}>
Expand All @@ -143,7 +163,7 @@ const GridControls = () => {
<Button onSuccess={clearProgress} requireConfirmation={true}>
Clear All
</Button>
</>
</div>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export const MultiplePlayersRow: StoryFn = () => {
export const CustomLayoutRaw: StoryFn = () => {
return (
<Crossword data={data} progress={progress}>
<Crossword.Grid />
<Crossword.Controls />
<Crossword.InteractiveGrid />
<Crossword.SavedMessage />
<Crossword.Clues direction="across" />
<Crossword.Clues direction="down" />
Expand Down Expand Up @@ -108,10 +107,7 @@ export const CustomisedLayout: StoryFn = () => {
/>
</div>
<div style={{ flexBasis: 496, minWidth: '15em' }}>
<Crossword.Grid />
<div style={{ display: 'flex', flexDirection: 'column', gap: 5 }}>
<Crossword.Controls />
</div>
<Crossword.InteractiveGrid />
<div
style={{
fontFamily: 'cursive',
Expand Down
14 changes: 3 additions & 11 deletions libs/@guardian/react-crossword/src/components/Crossword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type { Progress, Theme } from '../@types/crossword';
import { ContextProvider } from '../context/ContextProvider';
import { useProgress } from '../context/Progress';
import { Clues } from './Clues';
import { Controls } from './Controls';
import { Grid } from './Grid';
import { InteractiveGrid } from './InteractiveGrid';
import { Layout } from './Layout';

export type CrosswordProps = {
Expand Down Expand Up @@ -59,13 +58,7 @@ export const Crossword = ({
{children ?? (
<Layout.Wrapper>
<Layout.Grid>
<Grid />
<Layout.Controls>
<Controls.Clues />
</Layout.Controls>
<Layout.Controls>
<Controls.Grid />
</Layout.Controls>
<InteractiveGrid />
<Layout.SavedMessage>
<SavedMessage />
</Layout.SavedMessage>
Expand All @@ -87,7 +80,6 @@ export const Crossword = ({
);
};

Crossword.Grid = Grid;
Crossword.InteractiveGrid = InteractiveGrid;
Crossword.Clues = Clues;
Crossword.Controls = Controls;
Crossword.SavedMessage = SavedMessage;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { memo } from 'react';
import { Controls } from './Controls';
import { Grid } from './Grid';

export const InteractiveGrid = memo(() => {
return (
<>
<Grid />
<Controls.Clues />
<Controls.Grid />
</>
);
});
39 changes: 2 additions & 37 deletions libs/@guardian/react-crossword/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@ import { css } from '@emotion/react';
import { headlineBold17, space } from '@guardian/source/foundations';
import { textSans12, textSans14 } from '@guardian/source/foundations';
import type { ReactNode } from 'react';
import { useMemo } from 'react';
import { memo } from 'react';
import type { Direction } from '../@types/Direction';
import { useData } from '../context/Data';
import { useTheme } from '../context/Theme';

const useGridWidth = () => {
const { gutter, cellSize } = useTheme();
const { dimensions } = useData();

return useMemo(
() => Math.max((cellSize + gutter) * dimensions.cols + gutter, 300),
[cellSize, gutter, dimensions.cols],
);
};

const useWidthForCols = (cols: number) => {
const gridWidth = useGridWidth();
const { clueMinWidth } = useTheme();

return gridWidth + clueMinWidth * cols + 'px';
};
import { useGridWidth } from '../hooks/useGridWidth';
import { useWidthForCols } from '../hooks/useWidthForCols';

export const Wrapper = memo(({ children }: { children: ReactNode }) => {
const { text, clueMaxWidth } = useTheme();
Expand Down Expand Up @@ -125,23 +108,6 @@ const CluesHeader = memo(({ direction }: { direction: Direction }) => {
);
});

const Controls = memo(({ children }: { children: ReactNode }) => {
return (
<div
css={css`
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
gap: ${space[1]}px;
padding: ${space[1]}px 0;
`}
>
{children}
</div>
);
});

const SavedMessage = memo(({ children }: { children: ReactNode }) => {
const theme = useTheme();

Expand All @@ -163,6 +129,5 @@ export const Layout = {
Grid,
Clues,
CluesHeader,
Controls,
SavedMessage,
};
13 changes: 13 additions & 0 deletions libs/@guardian/react-crossword/src/hooks/useGridWidth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useMemo } from 'react';
import { useData } from '../context/Data';
import { useTheme } from '../context/Theme';

export const useGridWidth = () => {
const { gutter, cellSize } = useTheme();
const { dimensions } = useData();

return useMemo(
() => Math.max((cellSize + gutter) * dimensions.cols + gutter, 300),
[cellSize, gutter, dimensions.cols],
);
};
9 changes: 9 additions & 0 deletions libs/@guardian/react-crossword/src/hooks/useWidthForCols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useTheme } from '../context/Theme';
import { useGridWidth } from './useGridWidth';

export const useWidthForCols = (cols: number) => {
const gridWidth = useGridWidth();
const { clueMinWidth } = useTheme();

return gridWidth + clueMinWidth * cols + 'px';
};
Loading