Skip to content

Commit

Permalink
refactor(appbar): add position prop
Browse files Browse the repository at this point in the history
This also deprecates the fixed prop.
  • Loading branch information
WesSouza authored and arturbien committed Aug 5, 2022
1 parent e3c0338 commit 74f56ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/AppBar/AppBar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ describe('<AppBar />', () => {
expect(headerEl).toHaveStyleRule('position', 'absolute');
});

it('should render position prop properly', () => {
const { container } = render(
<AppBar {...defaultProps} position='sticky' />
);
const headerEl = container.firstElementChild;

expect(headerEl).toHaveStyleRule('position', 'sticky');
});

it('should custom style', () => {
const { container } = render(
<AppBar {...defaultProps} style={{ backgroundColor: 'papayawhip' }} />
Expand Down
15 changes: 11 additions & 4 deletions src/AppBar/AppBar.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React, { forwardRef } from 'react';
import styled from 'styled-components';
import styled, { CSSProperties } from 'styled-components';

import { createBorderStyles, createBoxStyles } from '../common';
import { CommonStyledProps } from '../types';

type AppBarProps = {
children: React.ReactNode;
/** @deprecated Use `position` instead */
fixed?: boolean;
position?: CSSProperties['position'];
} & React.HTMLAttributes<HTMLElement> &
CommonStyledProps;

const StyledAppBar = styled.header<AppBarProps>`
${createBorderStyles()};
${createBoxStyles()};
position: ${props => (props.fixed ? 'fixed' : 'absolute')};
position: ${props => props.position ?? (props.fixed ? 'fixed' : 'absolute')};
top: 0;
right: 0;
left: auto;
Expand All @@ -24,9 +26,14 @@ const StyledAppBar = styled.header<AppBarProps>`
`;

const AppBar = forwardRef<HTMLElement, AppBarProps>(
({ children, fixed = true, ...otherProps }, ref) => {
({ children, fixed = true, position = 'fixed', ...otherProps }, ref) => {
return (
<StyledAppBar fixed={fixed} ref={ref} {...otherProps}>
<StyledAppBar
fixed={fixed}
position={fixed !== false ? position : undefined}
ref={ref}
{...otherProps}
>
{children}
</StyledAppBar>
);
Expand Down

0 comments on commit 74f56ec

Please sign in to comment.