Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
fix(Table): Row headers on mobile aren't sticky anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
diondiondion committed Oct 10, 2019
1 parent 87fd2ee commit 14dca37
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
68 changes: 33 additions & 35 deletions src/Table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import styled, {css} from 'styled-components';

import {pxToRem} from '../utils/units';
import {alpha} from '../utils/colors';
import {alpha, getSolidBackgroundShade} from '../utils/colors';
import {borderValue, overflowWrap} from '../mixins';
import {positionProps, marginProps} from '../styleProps';
import {getSpacing} from '../utils/spacing';
Expand All @@ -17,37 +17,6 @@ function getBreakpoint(key) {
return p => p.theme.globals.breakpoints[p[key]];
}

const stickyHeaderStyles = css`
position: sticky;
top: ${p => pxToRem(p.stickyHeaderOffset) || 0};
z-index: ${p => p.theme.globals.z.raised};
background-color: ${p => p.theme.background};
/**
* MS Edge shows glitchy rendering when using a normal
* border on sticky table headers, so we're using a
* pseudo-element instead
*/
&::after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: ${p => p.theme.globals.z.below};
border-bottom: ${p => borderValue(p.theme)};
${p =>
p.shadedHeader &&
css`
background-color: ${alpha(
p.theme.shade,
p.theme.shadeStrength
)};
`}
}
`;

const StyledTable = styled.table`
position: relative;
${positionProps}
Expand All @@ -73,7 +42,32 @@ const StyledTable = styled.table`
}
thead th {
${stickyHeaderStyles}
position: relative;
position: sticky;
top: ${p => pxToRem(p.stickyHeaderOffset) || 0};
z-index: ${p => p.theme.globals.z.raised};
background-color: ${p => p.theme.background};
/**
* MS Edge shows glitchy rendering when using a normal
* border on sticky table headers, so we're using a
* pseudo-element instead
*/
&::after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: ${p => p.theme.globals.z.below};
border-bottom: ${p => borderValue(p.theme)};
${p =>
p.shadedHeader &&
css`
background-color: ${p => getSolidBackgroundShade(p.theme)};
`}
}
}
caption {
Expand Down Expand Up @@ -167,8 +161,6 @@ const StyledTable = styled.table`
/* Make sure to display the header at the top */
order: -1;
${stickyHeaderStyles}
/* Flex to vertically align content in cell */
display: flex;
align-items: center;
Expand All @@ -177,6 +169,12 @@ const StyledTable = styled.table`
/* Visually, this margin is added to the top padding
* of the content area */
margin-bottom: ${p => p.theme.globals.spacing.xs};
border-bottom: ${p => borderValue(p.theme)};
background-color: ${p =>
p.shadedHeader
? getSolidBackgroundShade(p.theme)
: p.theme.background};
}
td {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ function getColorBlock(color, theme) {
return theme[color] || theme.globals.colorBlocks[color] || color;
}

function getBackgroundShade(theme) {
return alpha(theme.shade, theme.shadeStrength);
}

function getSolidBackgroundShade(theme) {
return mix(theme.shade, 'rgb')(theme.background, theme.shadeStrength);
}

export {
alpha,
mix,
Expand All @@ -43,4 +51,6 @@ export {
isDark,
isLight,
getColorBlock,
getBackgroundShade,
getSolidBackgroundShade,
};

0 comments on commit 14dca37

Please sign in to comment.