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

Replace deprecated Button in Dialog #4049

Merged
merged 8 commits into from
Dec 19, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/eight-pillows-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Replaces deprecated `Button` component in `Dialog` with newest version
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions src/ConfirmationDialog/ConfirmationDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,33 @@ describe('ConfirmationDialog', () => {
})

it('focuses the primary action when opened and the confirmButtonType is not set', async () => {
const {getByText} = HTMLRender(<Basic />)
const {getByText, getByRole} = HTMLRender(<Basic />)
fireEvent.click(getByText('Show dialog'))
expect(getByText('Primary')).toEqual(document.activeElement)
expect(getByText('Secondary')).not.toEqual(document.activeElement)
expect(getByRole('button', {name: 'Primary'})).toEqual(document.activeElement)
expect(getByRole('button', {name: 'Secondary'})).not.toEqual(document.activeElement)
})

it('focuses the primary action when opened and the confirmButtonType is not danger', async () => {
const {getByText} = HTMLRender(<Basic confirmButtonType="primary" />)
const {getByText, getByRole} = HTMLRender(<Basic confirmButtonType="primary" />)
fireEvent.click(getByText('Show dialog'))
expect(getByText('Primary')).toEqual(document.activeElement)
expect(getByText('Secondary')).not.toEqual(document.activeElement)
expect(getByRole('button', {name: 'Primary'})).toEqual(document.activeElement)
expect(getByRole('button', {name: 'Secondary'})).not.toEqual(document.activeElement)
})

it('focuses the secondary action when opened and the confirmButtonType is danger', async () => {
const {getByText} = HTMLRender(<Basic confirmButtonType="danger" />)
const {getByText, getByRole} = HTMLRender(<Basic confirmButtonType="danger" />)
fireEvent.click(getByText('Show dialog'))
expect(getByText('Primary')).not.toEqual(document.activeElement)
expect(getByText('Secondary')).toEqual(document.activeElement)
expect(getByRole('button', {name: 'Primary'})).not.toEqual(document.activeElement)
expect(getByRole('button', {name: 'Secondary'})).toEqual(document.activeElement)
})

it('supports nested `focusTrap`s', async () => {
const {getByText} = HTMLRender(<ShorthandHookFromActionMenu />)
const {getByText, getByRole} = HTMLRender(<ShorthandHookFromActionMenu />)

fireEvent.click(getByText('Show menu'))
fireEvent.click(getByText('Show dialog'))

expect(getByText('Primary')).toEqual(document.activeElement)
expect(getByText('Secondary')).not.toEqual(document.activeElement)
expect(getByRole('button', {name: 'Primary'})).toEqual(document.activeElement)
expect(getByRole('button', {name: 'Secondary'})).not.toEqual(document.activeElement)
})
})
4 changes: 2 additions & 2 deletions src/Dialog/Dialog.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const Default = () => {
title="My Dialog"
onClose={onDialogClose}
footerButtons={[
{buttonType: 'normal', content: 'Open Second Dialog', onClick: openSecondDialog},
{buttonType: 'default', content: 'Open Second Dialog', onClick: openSecondDialog},
{buttonType: 'danger', content: 'Delete the universe', onClick: onDialogClose},
{buttonType: 'primary', content: 'Proceed', onClick: openSecondDialog, autoFocus: true},
]}
Expand Down Expand Up @@ -126,7 +126,7 @@ export const Playground = ({width, height, subtitle}: DialogStoryProps) => {
width={width}
height={height}
footerButtons={[
{buttonType: 'normal', content: 'Open Second Dialog', onClick: openSecondDialog},
{buttonType: 'default', content: 'Open Second Dialog', onClick: openSecondDialog},
{buttonType: 'danger', content: 'Delete the universe', onClick: onDialogClose},
{buttonType: 'primary', content: 'Proceed', onClick: openSecondDialog, autoFocus: true},
]}
Expand Down
19 changes: 7 additions & 12 deletions src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useCallback, useEffect, useRef, useState} from 'react'
import styled from 'styled-components'
import Button, {ButtonPrimary, ButtonDanger, ButtonProps} from '../deprecated/Button'
import {Button, ButtonProps} from '../Button'
import Box from '../Box'
import {get} from '../constants'
import {useOnEscapePress, useProvidedRefOrCreate} from '../hooks'
Expand All @@ -27,7 +27,7 @@ export type DialogButtonProps = Omit<ButtonProps, 'content'> & {
/**
* The type of Button element to use
*/
buttonType?: 'normal' | 'primary' | 'danger'
buttonType?: 'default' | 'primary' | 'danger' | 'normal'

/**
* The Button's inner text
Expand Down Expand Up @@ -381,11 +381,6 @@ const Footer = styled.div<SxProp>`
${sx};
`

const buttonTypes = {
normal: Button,
primary: ButtonPrimary,
danger: ButtonDanger,
}
const Buttons: React.FC<React.PropsWithChildren<{buttons: DialogButtonProps[]}>> = ({buttons}) => {
const autoFocusRef = useProvidedRefOrCreate<HTMLButtonElement>(buttons.find(button => button.autoFocus)?.ref)
let autoFocusCount = 0
Expand All @@ -402,17 +397,17 @@ const Buttons: React.FC<React.PropsWithChildren<{buttons: DialogButtonProps[]}>>
return (
<>
{buttons.map((dialogButtonProps, index) => {
const {content, buttonType = 'normal', autoFocus = false, ...buttonProps} = dialogButtonProps
const ButtonElement = buttonTypes[buttonType]
const {content, buttonType = 'default', autoFocus = false, ...buttonProps} = dialogButtonProps
return (
<ButtonElement
<Button
key={index}
{...buttonProps}
variant={buttonType}
// 'normal' value is equivalent to 'default', this is used for backwards compatibility
variant={buttonType === 'normal' ? 'default' : buttonType}
ref={autoFocus && autoFocusCount === 0 ? (autoFocusCount++, autoFocusRef) : null}
>
{content}
</ButtonElement>
</Button>
)
})}
</>
Expand Down
36 changes: 18 additions & 18 deletions src/__tests__/__snapshots__/AnchoredOverlay.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ exports[`AnchoredOverlay should render consistently when open 1`] = `
color: #1F2328;
}

.c3 {
background-color: #ffffff;
box-shadow: 0 1px 3px rgba(31,35,40,0.12),0 8px 24px rgba(66,74,83,0.12);
position: absolute;
min-width: 192px;
height: auto;
width: auto;
border-radius: 12px;
overflow: hidden;
-webkit-animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
visibility: var(--styled-overlay-visibility);
}

.c3:focus {
outline: none;
}

.c1 {
position: relative;
display: inline-block;
Expand Down Expand Up @@ -170,24 +188,6 @@ exports[`AnchoredOverlay should render consistently when open 1`] = `
border-color: rgba(31,35,40,0.15);
}

.c3 {
background-color: #ffffff;
box-shadow: 0 1px 3px rgba(31,35,40,0.12),0 8px 24px rgba(66,74,83,0.12);
position: absolute;
min-width: 192px;
height: auto;
width: auto;
border-radius: 12px;
overflow: hidden;
-webkit-animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
visibility: var(--styled-overlay-visibility);
}

.c3:focus {
outline: none;
}

@media (forced-colors:active) {
.c3 {
outline: solid 1px transparent;
Expand Down
Loading