Skip to content

Commit

Permalink
feat: Focus handling for Cards and Chips (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmar authored Aug 28, 2023
1 parent 43cd883 commit 4dd9851
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/components/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Div, type DivProps } from 'honorable'
import { forwardRef, useMemo } from 'react'

import { styledTheme as theme } from '../theme'
import { useTheme } from 'styled-components'

import {
type FillLevel,
Expand Down Expand Up @@ -100,20 +99,31 @@ const Card = forwardRef<HTMLDivElement, CardProps>(
ref
) => {
fillLevel = useDecideFillLevel({ hue, fillLevel })
const theme = useTheme()

return (
<FillLevelProvider value={fillLevel}>
<Div
ref={ref}
{...theme.partials.reset.button}
border={`1px solid ${fillLevelToBorderColor[fillLevel]}`}
borderRadius={cornerSizeToBorderRadius[size]}
backgroundColor={
selected
? fillLevelToSelectedBGColor[fillLevel]
: fillLevelToBGColor[fillLevel]
}
{...{
'&:focus, &:focus-visible': {
outline: 'none',
},
'&:focus-visible': {
borderColor: theme.colors['border-outline-focused'],
},
}}
{...(clickable && {
cursor: 'pointer',
as: 'button',
})}
{...(clickable &&
!selected && {
Expand Down
12 changes: 10 additions & 2 deletions src/components/Chip.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Flex, type FlexProps, Spinner } from 'honorable'
import PropTypes from 'prop-types'
import { type ReactElement, type Ref, forwardRef } from 'react'
import {
type ComponentProps,
type ReactElement,
type Ref,
forwardRef,
} from 'react'
import styled, { type DefaultTheme } from 'styled-components'

import Card, { type BaseCardProps } from './Card'
Expand Down Expand Up @@ -93,8 +98,9 @@ function ChipRef(
icon,
closeButton,
clickable,
as,
...props
}: ChipProps,
}: ChipProps & { as?: ComponentProps<typeof ChipCard>['forwardedAs'] },
ref: Ref<any>
) {
const parentFillLevel = useFillLevel()
Expand All @@ -113,6 +119,8 @@ function ChipRef(
paddingHorizontal={size === 'small' ? 'xsmall' : 'small'}
alignItems="center"
display="inline-flex"
textDecoration="none"
{...(as ? { forwardedAs: as } : {})}
{...props}
>
{loading && (
Expand Down
9 changes: 8 additions & 1 deletion src/stories/Chip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { StatusOkIcon, WrapWithIf } from '..'
import Chip from '../components/Chip'
import Card from '../components/Card'

import { Link } from './NavigationContextStub'

export default {
title: 'Chip',
component: Chip,
Expand Down Expand Up @@ -47,7 +49,11 @@ const severities: ComponentProps<typeof Chip>['severity'][] = [

const versionsArgs = [{}, { loading: true }, { icon: <StatusOkIcon /> }]

function Template({ onFillLevel, ...args }: any) {
function Template({ onFillLevel, asLink, ...args }: any) {
if (asLink) {
args = { ...args, as: Link, href: '#' }
}

return (
<>
{/* Sizes */}
Expand Down Expand Up @@ -236,5 +242,6 @@ export const Default = Template.bind({})
Default.args = {
closeButton: false,
clickable: false,
asLink: false,
onFillLevel: 0,
}

0 comments on commit 4dd9851

Please sign in to comment.