-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
402 additions
and
79 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from "react"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { OptionCardsGroup, OptionCard } from "./OptionCard"; | ||
import { Text } from "../Typography/Text"; | ||
|
||
const meta: Meta<typeof OptionCardsGroup> = { | ||
title: 'Components/OptionCardsGroup', | ||
component: OptionCardsGroup, | ||
tags: ["autodocs"], | ||
args: { | ||
allowMultipleSelections: false, | ||
showTickIcon: true, | ||
}, | ||
parameters: { | ||
docs: { | ||
description: { | ||
component: "A group of option cards allowing for single or multiple selections.", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof OptionCardsGroup>; | ||
|
||
export const SingleSelection: Story = { | ||
render: (args) => ( | ||
<OptionCardsGroup {...args}> | ||
<OptionCard id="option1" padding="micro"> | ||
<Text>Option 1</Text> | ||
</OptionCard> | ||
<OptionCard id="option2" padding="micro"> | ||
<Text>Option 2</Text> | ||
</OptionCard> | ||
<OptionCard id="option3" padding="micro"> | ||
<Text>Option 3</Text> | ||
</OptionCard> | ||
</OptionCardsGroup> | ||
), | ||
args: { | ||
allowMultipleSelections: false, | ||
showTickIcon: true, | ||
}, | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: "A group of option cards configured for single selection.", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const MultipleSelection: Story = { | ||
render: (args) => ( | ||
<OptionCardsGroup {...args}> | ||
<OptionCard id="option1" padding="micro"> | ||
<Text>Option 1</Text> | ||
</OptionCard> | ||
<OptionCard id="option2" padding="micro"> | ||
<Text>Option 2</Text> | ||
</OptionCard> | ||
<OptionCard id="option3" padding="micro"> | ||
<Text>Option 3</Text> | ||
</OptionCard> | ||
</OptionCardsGroup> | ||
), | ||
args: { | ||
allowMultipleSelections: true, | ||
showTickIcon: true, | ||
}, | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: "A group of option cards configured for multiple selections.", | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import React, { createContext, useContext, useState, ReactNode, useCallback } from "react"; | ||
|
||
import { Element } from "../Element/Element"; | ||
import { Card, CardElementType, CardProps } from "../Card/Card"; | ||
|
||
import "./option-card.css"; | ||
|
||
// import TickIcon from "../../assets/icons/tick.svg"; | ||
|
||
export interface OptionCardsProviderProps { | ||
children : ReactNode; | ||
allowMultipleSelections ? : boolean; | ||
showTickIcon ? : boolean; | ||
onSelectionChange ? : (selectedIds: Set<string>) => void; | ||
} | ||
|
||
export interface OptionCardProps extends CardProps { | ||
id : string; | ||
children : ReactNode; | ||
disabled ? : boolean; | ||
} | ||
|
||
const OptionCardsContext = createContext<{ | ||
isSelected : (id: string) => boolean; | ||
toggleSelection : (id: string) => void; | ||
showTickIcon ? : boolean; | ||
}>({ | ||
isSelected : () => false, | ||
toggleSelection : () => {}, | ||
showTickIcon : false, | ||
}); | ||
|
||
export const OptionCardsGroup: React.FC<OptionCardsProviderProps> = ( | ||
{ | ||
children, | ||
allowMultipleSelections = false, | ||
showTickIcon, | ||
onSelectionChange, | ||
...props | ||
}, | ||
) => { | ||
const [ selectedIds, setSelectedIds ] = useState<Set<string>>(new Set()); | ||
|
||
const toggleSelection = useCallback((id: string) => { | ||
setSelectedIds(prevSelectedIds => { | ||
const newSelectedIds = new Set(prevSelectedIds); | ||
if (allowMultipleSelections) { | ||
if (newSelectedIds.has(id)) { | ||
newSelectedIds.delete(id); | ||
} else { | ||
newSelectedIds.add(id); | ||
} | ||
} else { | ||
if (newSelectedIds.has(id) && prevSelectedIds.size === 1) { | ||
newSelectedIds.clear(); | ||
} else { | ||
newSelectedIds.clear(); | ||
newSelectedIds.add(id); | ||
} | ||
} | ||
onSelectionChange?.(newSelectedIds); | ||
return newSelectedIds; | ||
}); | ||
}, [ allowMultipleSelections, onSelectionChange ]); | ||
|
||
const isSelected = useCallback((id: string) => { | ||
return selectedIds.has(id); | ||
}, [ selectedIds ]); | ||
|
||
return ( | ||
<OptionCardsContext.Provider value={{ isSelected, toggleSelection, showTickIcon }}> | ||
{children} | ||
</OptionCardsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useOptionCard = (id: string) => { | ||
const context = useContext(OptionCardsContext); | ||
return { | ||
isSelected : context.isSelected(id), | ||
toggleSelection : () => context.toggleSelection(id), | ||
showTickIcon : context.showTickIcon, | ||
}; | ||
}; | ||
|
||
export const OptionCard: React.FC<OptionCardProps> = ({ id, children, disabled = false, ...props }) => { | ||
const { isSelected, toggleSelection, showTickIcon } = useOptionCard(id); | ||
|
||
let classNames = []; | ||
|
||
if (isSelected) { | ||
classNames.push("selected"); | ||
} | ||
|
||
if (disabled) { | ||
classNames.push("disabled"); | ||
} | ||
|
||
const handleKeyDown = (e: React.KeyboardEvent) => { | ||
if ((e.key === "Enter" || e.key === " ") && !disabled) { | ||
e.preventDefault(); | ||
toggleSelection(); | ||
} | ||
}; | ||
|
||
const handleClick = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => { | ||
if (!disabled) { | ||
toggleSelection(); | ||
props.onClick?.(e); | ||
} | ||
}; | ||
|
||
return ( | ||
<Element<CardElementType> | ||
as={Card} | ||
data-option-card | ||
role="button" | ||
tabIndex={disabled ? -1 : 0} | ||
aria-disabled={disabled ? "true" : "false"} | ||
aria-selected={isSelected ? "true" : "false"} | ||
classNames={classNames} | ||
onClick={handleClick} | ||
onKeyDown={handleKeyDown} | ||
{...props} | ||
> | ||
{showTickIcon | ||
? ( | ||
<svg viewBox="0 0 24 24" className="tick-icon"> | ||
<circle cx="12" cy="12" r="11" /> | ||
<path d="M8 13 L11 15 L16 9" /> | ||
</svg> | ||
) : null} | ||
{children} | ||
</Element> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { OptionCard, OptionCardsGroup, type OptionCardProps } from "./OptionCard"; | ||
export { useOptionCard } from "./OptionCard"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
[data-option-cards-group] { | ||
width : 100%; | ||
} | ||
|
||
[data-option-card] { | ||
user-select : none; | ||
transition : all 0.2s; | ||
|
||
&.disabled { | ||
filter : grayscale(24%); | ||
opacity : 0.6; | ||
cursor : not-allowed; | ||
} | ||
|
||
&:not(.disabled) { cursor : pointer; } | ||
|
||
&:hover { | ||
z-index : 500; | ||
border : var(--option-card-border-width) solid var(--option-card-border-hover); | ||
background : var(--option-card-bg-hover); | ||
box-shadow : 0 1px 2px rgba(0, 0, 0, 0.04), 0 12px 24px rgba(0, 0, 0, 0.08); | ||
|
||
.tick-icon { | ||
display : block; | ||
|
||
circle { fill : var(--option-card-tick-bg-hover); } | ||
path { stroke : var(--option-card-tick-line-hover); } | ||
} | ||
} | ||
|
||
&:active { box-shadow : none; } | ||
|
||
&.selected { | ||
background-color : var(--option-card-bg-selected); | ||
border : var(--option-card-border-width) solid var(--option-card-border-selected); | ||
|
||
.tick-icon { | ||
display : block; | ||
} | ||
} | ||
|
||
/* Here’s a clever bit of UX */ | ||
&.selected:hover { | ||
.tick-icon circle { fill : var(--option-card-tick-bg-selected); } | ||
.tick-icon path {stroke : var(--option-card-tick-line-selected); } | ||
} | ||
|
||
&:focus { | ||
outline : 2px solid var(--option-card-border-hover); | ||
outline-color : var(--radio-tabs-label-focus-border); | ||
} | ||
|
||
/* THE TICK ============================================================= */ | ||
.tick-icon { | ||
display : none; | ||
position : absolute; | ||
top : 8px; | ||
right : 8px; | ||
width : 24px; | ||
height : 24px; | ||
transition : all 0.2s; | ||
} | ||
|
||
.tick-icon circle { fill : var(--option-card-tick-bg-selected); } | ||
|
||
.tick-icon path { | ||
stroke : var(--option-card-tick-line-selected); | ||
stroke-width : 2px; | ||
fill : none; | ||
stroke-linecap : round; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.