-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Creates internal drawer component (#1495)
Co-authored-by: Katie George <[email protected]>
- Loading branch information
1 parent
e73af60
commit 82b4c9c
Showing
17 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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,49 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import createWrapper from '../../../../../lib/components/test-utils/dom'; | ||
import Drawer from '../../../../../lib/components/internal/components/drawer'; | ||
import TestI18nProvider from '../../../../../lib/components/i18n/testing'; | ||
import styles from '../../../../../lib/components/internal/components/drawer/styles.css.js'; | ||
|
||
function renderDrawer(jsx: React.ReactElement) { | ||
const { container } = render(jsx); | ||
const wrapper = createWrapper(container).findByClassName(styles.drawer)!; | ||
const header = wrapper.findByClassName(styles.header); | ||
const content = wrapper.findByClassName(styles['test-utils-drawer-content']); | ||
return { wrapper, header, content }; | ||
} | ||
|
||
test('renders only children by default', () => { | ||
const { header, content } = renderDrawer(<Drawer>test content</Drawer>); | ||
|
||
expect(header).toBeNull(); | ||
expect(content!.getElement()).toHaveTextContent('test content'); | ||
}); | ||
|
||
test('renders header if it is provided', () => { | ||
const { header, content } = renderDrawer(<Drawer header="Bla bla">there is a header above</Drawer>); | ||
expect(header!.getElement()).toHaveTextContent('Bla bla'); | ||
expect(content!.getElement()).toHaveTextContent('there is a header above'); | ||
}); | ||
|
||
test('renders loading state', () => { | ||
const { wrapper, header, content } = renderDrawer( | ||
<Drawer loading={true} i18nStrings={{ loadingText: 'Loading content' }} /> | ||
); | ||
expect(wrapper.findStatusIndicator()!.getElement()).toHaveTextContent('Loading content'); | ||
expect(header).toBeNull(); | ||
expect(content).toBeNull(); | ||
}); | ||
|
||
describe('i18n', () => { | ||
test('supports providing loadingText from i18n provider', () => { | ||
const { container } = render( | ||
<TestI18nProvider messages={{ drawer: { 'i18nStrings.loadingText': 'Custom loading text' } }}> | ||
<Drawer loading={true} /> | ||
</TestI18nProvider> | ||
); | ||
expect(createWrapper(container).findStatusIndicator()!.getElement()).toHaveTextContent('Custom loading text'); | ||
}); | ||
}); |
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,31 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import clsx from 'clsx'; | ||
import React from 'react'; | ||
import { getBaseProps } from '../../base-component'; | ||
import InternalStatusIndicator from '../../../status-indicator/internal'; | ||
import styles from './styles.css.js'; | ||
import { DrawerProps } from './interfaces'; | ||
import LiveRegion from '../../components/live-region'; | ||
import { useInternalI18n } from '../../../i18n/context'; | ||
|
||
export default function Drawer({ header, children, loading, i18nStrings, ...restProps }: DrawerProps) { | ||
const baseProps = getBaseProps(restProps); | ||
const i18n = useInternalI18n('drawer'); | ||
const containerProps = { | ||
...baseProps, | ||
className: clsx(baseProps.className, styles.drawer), | ||
}; | ||
return loading ? ( | ||
<div {...containerProps}> | ||
<InternalStatusIndicator type="loading"> | ||
<LiveRegion visible={true}>{i18n('i18nStrings.loadingText', i18nStrings?.loadingText)}</LiveRegion> | ||
</InternalStatusIndicator> | ||
</div> | ||
) : ( | ||
<div {...containerProps}> | ||
{header && <div className={clsx(styles.header)}>{header}</div>} | ||
<div className={clsx(styles['test-utils-drawer-content'])}>{children}</div> | ||
</div> | ||
); | ||
} |
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,37 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { BaseComponentProps } from '../../base-component'; | ||
import React from 'react'; | ||
|
||
export interface DrawerProps extends BaseComponentProps { | ||
/** | ||
* Header of the drawer. | ||
* | ||
* It should contain the only `h2` used in the drawer. | ||
*/ | ||
header?: React.ReactNode; | ||
|
||
/** | ||
* Main content of the drawer. | ||
* | ||
*/ | ||
children?: React.ReactNode; | ||
|
||
/** | ||
* Renders the drawer in a loading state. We recommend that you also set a `loadingText`. | ||
*/ | ||
loading?: boolean; | ||
|
||
/** | ||
* An object containing all the necessary localized strings required by the component. | ||
* @i18n | ||
*/ | ||
i18nStrings?: I18nStrings; | ||
} | ||
|
||
export interface I18nStrings { | ||
/** | ||
Specifies the text that's displayed when the panel is in a loading state. | ||
*/ | ||
loadingText?: string; | ||
} |
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,41 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* stylelint-disable @cloudscape-design/no-implicit-descendant, selector-max-type */ | ||
@use '../../styles/tokens' as awsui; | ||
@use '../../styles' as styles; | ||
|
||
.drawer { | ||
@include styles.styles-reset; | ||
word-wrap: break-word; | ||
padding: awsui.$space-scaled-l awsui.$space-panel-side-right awsui.$space-scaled-xxxl awsui.$space-panel-side-left; | ||
} | ||
|
||
.header { | ||
@include styles.font-panel-header; | ||
color: awsui.$color-text-heading-default; | ||
padding-bottom: awsui.$space-scaled-l; | ||
padding-left: awsui.$space-panel-side-left; | ||
// padding to make sure the header doesn't overlap with the close icon | ||
padding-right: calc(#{awsui.$space-xl} + #{awsui.$space-scaled-xxl}); | ||
border-bottom: awsui.$border-divider-section-width solid awsui.$color-border-divider-default; | ||
margin: 0 calc(-1 * #{awsui.$space-panel-side-right}) awsui.$space-scaled-l calc(-1 * #{awsui.$space-panel-side-left}); | ||
|
||
h2, | ||
h3, | ||
h4, | ||
h5, | ||
h6 { | ||
@include styles.font-panel-header; | ||
padding-top: 0; | ||
padding-bottom: 0; | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
} | ||
} | ||
|
||
.test-utils-drawer-content { | ||
/* used in test-utils */ | ||
} |