-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Placeholder component (#5974)
- Loading branch information
Showing
18 changed files
with
534 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers'; | ||
import usePlaceholder, { UsePlaceholderProps } from './usePlaceholder'; | ||
import PlaceholderButton from './PlaceholderButton'; | ||
|
||
export interface PlaceholderProps extends UsePlaceholderProps, BsPrefixProps {} | ||
|
||
const propTypes = { | ||
/** | ||
* @default 'placeholder' | ||
*/ | ||
bsPrefix: PropTypes.string, | ||
|
||
/** | ||
* Changes the animation of the placeholder. | ||
* | ||
* @type ('glow'|'wave') | ||
*/ | ||
animation: PropTypes.string, | ||
|
||
/** | ||
* Change the background color of the placeholder. | ||
* | ||
* @type {('primary'|'secondary'|'success'|'danger'|'warning'|'info'|'light'|'dark')} | ||
*/ | ||
bg: PropTypes.string, | ||
|
||
/** | ||
* Component size variations. | ||
* | ||
* @type ('xs'|'sm'|'lg') | ||
*/ | ||
size: PropTypes.string, | ||
}; | ||
|
||
const Placeholder: BsPrefixRefForwardingComponent<'span', PlaceholderProps> = | ||
React.forwardRef<HTMLElement, PlaceholderProps>( | ||
({ as: Component = 'span', ...props }, ref) => { | ||
const placeholderProps = usePlaceholder(props); | ||
|
||
return <Component {...placeholderProps} ref={ref} />; | ||
}, | ||
); | ||
|
||
Placeholder.displayName = 'Placeholder'; | ||
Placeholder.propTypes = propTypes; | ||
|
||
export default Object.assign(Placeholder, { | ||
Button: PlaceholderButton, | ||
}); |
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,45 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { BsPrefixRefForwardingComponent } from './helpers'; | ||
import Button from './Button'; | ||
import usePlaceholder, { UsePlaceholderProps } from './usePlaceholder'; | ||
import { ButtonVariant } from './types'; | ||
|
||
export interface PlaceholderButtonProps extends UsePlaceholderProps { | ||
variant?: ButtonVariant; | ||
} | ||
|
||
const propTypes = { | ||
/** | ||
* @default 'placeholder' | ||
*/ | ||
bsPrefix: PropTypes.string, | ||
|
||
/** | ||
* Changes the animation of the placeholder. | ||
*/ | ||
animation: PropTypes.oneOf(['glow', 'wave']), | ||
|
||
size: PropTypes.oneOf(['xs', 'sm', 'lg']), | ||
|
||
/** | ||
* Button variant. | ||
*/ | ||
variant: PropTypes.string, | ||
}; | ||
|
||
const PlaceholderButton: BsPrefixRefForwardingComponent< | ||
'button', | ||
PlaceholderButtonProps | ||
> = React.forwardRef<HTMLButtonElement, PlaceholderButtonProps>( | ||
(props, ref) => { | ||
const placeholderProps = usePlaceholder(props); | ||
|
||
return <Button {...placeholderProps} ref={ref} disabled tabIndex={-1} />; | ||
}, | ||
); | ||
|
||
PlaceholderButton.displayName = 'PlaceholderButton'; | ||
PlaceholderButton.propTypes = propTypes; | ||
|
||
export default PlaceholderButton; |
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,34 @@ | ||
import classNames from 'classnames'; | ||
import { useBootstrapPrefix } from './ThemeProvider'; | ||
import { useCol, ColProps } from './Col'; | ||
import { Variant } from './types'; | ||
|
||
export type PlaceholderAnimation = 'glow' | 'wave'; | ||
export type PlaceholderSize = 'xs' | 'sm' | 'lg'; | ||
|
||
export interface UsePlaceholderProps extends Omit<ColProps, 'as'> { | ||
animation?: PlaceholderAnimation; | ||
bg?: Variant; | ||
size?: PlaceholderSize; | ||
} | ||
|
||
export default function usePlaceholder({ | ||
animation, | ||
bg, | ||
bsPrefix, | ||
size, | ||
...props | ||
}: UsePlaceholderProps) { | ||
bsPrefix = useBootstrapPrefix(bsPrefix, 'placeholder'); | ||
const [{ className, ...colProps }] = useCol(props); | ||
|
||
return { | ||
...colProps, | ||
className: classNames( | ||
className, | ||
animation ? `${bsPrefix}-${animation}` : bsPrefix, | ||
size && `${bsPrefix}-${size}`, | ||
bg && `bg-${bg}`, | ||
), | ||
}; | ||
} |
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,20 @@ | ||
import { render } from '@testing-library/react'; | ||
|
||
import PlaceholderButton from '../src/PlaceholderButton'; | ||
|
||
describe('<PlaceholderButton>', () => { | ||
it('should render a placeholder', () => { | ||
const { container } = render(<PlaceholderButton />); | ||
container.firstElementChild!.className.should.contain('placeholder'); | ||
}); | ||
|
||
it('should render size', () => { | ||
const { container } = render(<PlaceholderButton size="lg" />); | ||
container.firstElementChild!.className.should.contain('placeholder-lg'); | ||
}); | ||
|
||
it('should render animation', () => { | ||
const { container } = render(<PlaceholderButton animation="glow" />); | ||
container.firstElementChild!.className.should.contain('placeholder-glow'); | ||
}); | ||
}); |
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,25 @@ | ||
import { render } from '@testing-library/react'; | ||
|
||
import Placeholder from '../src/Placeholder'; | ||
|
||
describe('<Placeholder>', () => { | ||
it('should render a placeholder', () => { | ||
const { container } = render(<Placeholder />); | ||
container.firstElementChild!.className.should.contain('placeholder'); | ||
}); | ||
|
||
it('should render size', () => { | ||
const { container } = render(<Placeholder size="lg" />); | ||
container.firstElementChild!.className.should.contain('placeholder-lg'); | ||
}); | ||
|
||
it('should render animation', () => { | ||
const { container } = render(<Placeholder animation="glow" />); | ||
container.firstElementChild!.className.should.contain('placeholder-glow'); | ||
}); | ||
|
||
it('should render bg', () => { | ||
const { container } = render(<Placeholder bg="primary" />); | ||
container.firstElementChild!.className.should.contain('bg-primary'); | ||
}); | ||
}); |
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,8 @@ | ||
<> | ||
<Placeholder as="p" animation="glow"> | ||
<Placeholder xs={12} /> | ||
</Placeholder> | ||
<Placeholder as="p" animation="wave"> | ||
<Placeholder xs={12} /> | ||
</Placeholder> | ||
</>; |
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,27 @@ | ||
<div className="d-flex justify-content-around"> | ||
<Card style={{ width: '18rem' }}> | ||
<Card.Img variant="top" src="holder.js/100px180" /> | ||
<Card.Body> | ||
<Card.Title>Card Title</Card.Title> | ||
<Card.Text> | ||
Some quick example text to build on the card title and make up the bulk | ||
of the card's content. | ||
</Card.Text> | ||
<Button variant="primary">Go somewhere</Button> | ||
</Card.Body> | ||
</Card> | ||
|
||
<Card style={{ width: '18rem' }}> | ||
<Card.Img variant="top" src="holder.js/100px180" /> | ||
<Card.Body> | ||
<Placeholder as={Card.Title} animation="glow"> | ||
<Placeholder xs={6} /> | ||
</Placeholder> | ||
<Placeholder as={Card.Text} animation="glow"> | ||
<Placeholder xs={7} /> <Placeholder xs={4} /> <Placeholder xs={4} />{' '} | ||
<Placeholder xs={6} /> <Placeholder xs={8} /> | ||
</Placeholder> | ||
<Placeholder.Button variant="primary" xs={6} /> | ||
</Card.Body> | ||
</Card> | ||
</div>; |
Oops, something went wrong.