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

feat: actionButton on Banner #156

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
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: pnpm/[email protected].0
- uses: pnpm/[email protected].1
with:
version: 8.11.0

Expand All @@ -39,7 +39,7 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: pnpm/[email protected].0
- uses: pnpm/[email protected].1
with:
version: 8.11.0

Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: pnpm/[email protected].0
- uses: pnpm/[email protected].1
with:
version: 8.11.0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
ref: ${{ github.event.release.target_commitish }}

- uses: pnpm/[email protected].0
- uses: pnpm/[email protected].1
with:
version: 8.11.0

Expand Down
4 changes: 2 additions & 2 deletions components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
"ver": "pnpm npm version"
},
"dependencies": {
"@types/jest": "^29.5.12",
"clsx": "^1.1.1",
"focus-visible": "^5.2.0",
"jest-babel": "^1.0.1",
"lodash": "^4.17.21",
"ts-pattern": "^4.3.0"
},
Expand All @@ -56,6 +54,7 @@
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2",
"@types/glob": "^7.2.0",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.176",
"@types/node": "^16.11.6",
"@types/react": "^18.3.2",
Expand All @@ -68,6 +67,7 @@
"esbuild-darwin-arm64": "^0.14.27",
"glob": "^7.2.0",
"jest": "^29.7.0",
"jest-babel": "^1.0.1",
"jest-environment-jsdom": "^29.7.0",
"jest-styled-components": "^7.0.8",
"jest-watch-typeahead": "^1.0.0",
Expand Down
94 changes: 67 additions & 27 deletions components/src/components/atoms/Banner/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type BaseProps = {
actionIcon?: React.ReactNode
icon?: React.ReactNode
iconType?: IconTypes
actionButton?: React.ReactNode
} & NativeDivProps

type WithIcon = {
Expand All @@ -40,6 +41,7 @@ type WithAnchor = {
rel?: string
onDismiss?: never
actionIcon?: React.ReactNode
actionButton?: never
}

type WithoutAnchor = {
Expand All @@ -48,35 +50,38 @@ type WithoutAnchor = {
target?: never
rel?: never
onDismiss?: () => void
actionButton?: React.ReactNode
}

type NonNullableAlert = NonNullable<Props['alert']>

const Container = styled.div<{
$alert: NonNullableAlert
$hasAction: boolean
$hasBannerWideAction: boolean
}>(
({ theme, $alert, $hasAction }) => css`
({ theme, $alert, $hasBannerWideAction }) => css`
position: relative;
background: ${theme.colors.backgroundPrimary};
border: 1px solid ${theme.colors.border};
border-radius: ${theme.radii['2xLarge']};
padding: ${theme.space[4]};
display: flex;
align-items: stretch;
flex-direction: column;
gap: ${theme.space[4]};
width: ${theme.space.full};
transition: all 150ms ease-in-out;

${mq.sm.min(
css`
padding: ${theme.space['6']};
gap: ${theme.space[6]};
align-items: center;
flex-direction: row;
gap: ${theme.space[6]};
`,
)}

${$hasAction &&
${$hasBannerWideAction &&
css`
padding-right: ${theme.space[8]};
&:hover {
Expand Down Expand Up @@ -108,6 +113,16 @@ const Container = styled.div<{
)

const Content = styled.div(
({ theme }) => css`
flex: 1;
display: flex;
align-items: stretch;
gap: ${theme.space[4]};
width: ${theme.space.full};
`,
)

const TextContent = styled.div(
({ theme }) => css`
flex: 1;
display: flex;
Expand Down Expand Up @@ -165,7 +180,23 @@ const IconContainer = styled.div<{
`,
)

const ActionButtonContainer = styled.button(
const CustomActionButtonContainer = styled.div(
({ theme }) => css`
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-end;

& > * {
width: ${theme.space.full};
${mq.sm.min(css`
width: ${theme.space.fit};
`)}
}
`,
)

const ActionIconButtonContainer = styled.button(
({ theme }) => css`
position: absolute;
top: 0;
Expand All @@ -174,7 +205,7 @@ const ActionButtonContainer = styled.button(
`,
)

const ActionButtonIconWrapper = styled.div<{
const ActionIconButtonIconWrapper = styled.div<{
$alert: NonNullableAlert
$hasAction?: boolean
}>(
Expand Down Expand Up @@ -230,27 +261,28 @@ const ActionButtonIconWrapper = styled.div<{
`,
)

const ActionButton = ({
alert = 'info',
const ActionIconButton = ({
alert,
icon,
hasHref,
onDismiss,
}: Pick<Props, 'alert' | 'onDismiss'> & { hasHref: boolean } & WithIcon) => {
}: Required<WithAlert> &
Pick<Props, 'onDismiss'> & { hasHref: boolean } & WithIcon) => {
if (onDismiss)
return (
<ActionButtonContainer onClick={() => onDismiss()}>
<ActionButtonIconWrapper $alert={alert} $hasAction>
<ActionIconButtonContainer onClick={() => onDismiss()}>
<ActionIconButtonIconWrapper $alert={alert} $hasAction>
{icon || <CrossSVG />}
</ActionButtonIconWrapper>
</ActionButtonContainer>
</ActionIconButtonIconWrapper>
</ActionIconButtonContainer>
)
if (hasHref || icon)
return (
<ActionButtonContainer as="div">
<ActionButtonIconWrapper $alert={alert}>
<ActionIconButtonContainer as="div">
<ActionIconButtonIconWrapper $alert={alert}>
{icon || <UpRightArrowSVG />}
</ActionButtonIconWrapper>
</ActionButtonContainer>
</ActionIconButtonIconWrapper>
</ActionIconButtonContainer>
)
return null
}
Expand Down Expand Up @@ -282,6 +314,7 @@ export const Banner = React.forwardRef<
as: asProp,
children,
onDismiss,
actionButton,
...props
},
ref,
Expand All @@ -295,27 +328,34 @@ export const Banner = React.forwardRef<
))

const hasHref = !!props.href
const hasAction = hasHref || !!props.onClick
const hasBannerWideAction = hasHref || !!props.onClick
const _iconType = iconType || defaultIconType(alert, icon)

return (
<Container
{...props}
$alert={alert}
$hasAction={hasAction}
$hasBannerWideAction={hasBannerWideAction}
as={asProp as any}
ref={ref}
>
{_iconType !== 'none' && (
<IconContainer $alert={alert} $type={_iconType}>
{Icon}
</IconContainer>
)}
<Content>
{title && <Typography fontVariant="largeBold">{title}</Typography>}
<Typography>{children}</Typography>
{_iconType !== 'none' && (
<IconContainer $alert={alert} $type={_iconType}>
{Icon}
</IconContainer>
)}
<TextContent>
{title && <Typography fontVariant="largeBold">{title}</Typography>}
<Typography>{children}</Typography>
</TextContent>
</Content>
<ActionButton
{actionButton && (
<CustomActionButtonContainer>
<div>{actionButton}</div>
</CustomActionButtonContainer>
)}
<ActionIconButton
alert={alert}
hasHref={hasHref}
icon={props.actionIcon}
Expand Down
16 changes: 16 additions & 0 deletions docs/src/reference/mdx/atoms/Banner.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ import { Banner } from '@ensdomains/thorin'
</DeleteMe>
```

## Action Button

```tsx live=true
<DeleteMe>
<Banner alert="info" title="Heading" actionButton={<Button colorStyle="accentSecondary">Action</Button>}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</Banner>
<Banner alert="warning" title="Heading" actionButton={<Button colorStyle="yellowPrimary">Action</Button>}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</Banner>
<Banner alert="error" title="Heading" actionButton={<Button colorStyle="redPrimary">Action</Button>}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</Banner>
</DeleteMe>
```

## Icon Type

```tsx live=true
Expand Down
Loading
Loading