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

feat(tables): add isSticky prop to Head component #1482

Merged
merged 4 commits into from
Dec 15, 2022
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
16 changes: 8 additions & 8 deletions packages/tables/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"index.esm.js": {
"bundled": 24206,
"minified": 17389,
"gzipped": 4200,
"bundled": 24607,
"minified": 17705,
"gzipped": 4285,
"treeshaked": {
"rollup": {
"code": 13412,
"code": 13681,
"import_statements": 384
},
"webpack": {
"code": 14947
"code": 15238
}
}
},
"index.cjs.js": {
"bundled": 27224,
"minified": 19966,
"gzipped": 4518
"bundled": 27645,
"minified": 20302,
"gzipped": 4604
}
}
5 changes: 4 additions & 1 deletion packages/tables/demo/stories/TableStory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Row,
SortableCell,
ITableProps,
IHeadProps,
ISortableCellProps,
IRowProps
} from '@zendeskgarden/react-tables';
Expand All @@ -36,6 +37,7 @@ interface IArgs extends ITableProps {
isSortable: boolean;
isSelected?: IRowProps['isSelected'];
isStriped?: IRowProps['isStriped'];
isSticky?: IHeadProps['isSticky'];
isTruncated?: boolean;
}

Expand All @@ -49,6 +51,7 @@ export const TableStory: Story<IArgs> = ({
isSortable,
isSelected,
isStriped,
isSticky,
isTruncated,
...args
}) => {
Expand All @@ -71,7 +74,7 @@ export const TableStory: Story<IArgs> = ({
return (
<Table {...args}>
<Caption>{caption}</Caption>
<Head>
<Head isSticky={isSticky}>
<HeaderRow>
{hasSelection && (
<HeaderCell isMinimum>
Expand Down
1 change: 1 addition & 0 deletions packages/tables/demo/table.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { TABLE_DATA as DATA } from './stories/data';
isTruncated: { control: 'boolean', table: { category: 'Cell' } },
caption: { name: 'children', table: { category: 'Caption' } },
isBold: { control: 'boolean', table: { category: 'GroupRow' } },
isSticky: { control: 'boolean', table: { category: 'Head' } },
isStriped: { control: 'boolean', table: { category: 'Row' } },
isSelected: { control: 'boolean', table: { category: 'Row' } },
data: { name: 'Row[]', table: { category: 'Story' } },
Expand Down
9 changes: 5 additions & 4 deletions packages/tables/src/elements/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React, { HTMLAttributes, forwardRef } from 'react';
import React, { forwardRef } from 'react';
import { StyledHead } from '../styled';
import { IHeadProps } from '../types';

/**
* @extends HTMLAttributes<HTMLTableSectionElement>
*/
export const Head = forwardRef<HTMLTableSectionElement, HTMLAttributes<HTMLTableSectionElement>>(
(props, ref) => <StyledHead ref={ref} {...props} />
);
export const Head = forwardRef<HTMLTableSectionElement, IHeadProps>((props, ref) => (
<StyledHead ref={ref} {...props} />
));

Head.displayName = 'Head';
1 change: 1 addition & 0 deletions packages/tables/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type {
ITableProps,
IRowProps,
ICellProps,
IHeadProps,
IHeaderCellProps,
ISortableCellProps
} from './types';
39 changes: 39 additions & 0 deletions packages/tables/src/styled/StyledHead.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React from 'react';
import { render } from 'garden-test-utils';

import { StyledHead } from './StyledHead';
import { StyledHeaderRow } from './StyledHeaderRow';

describe('StyledHead', () => {
it('renders the expected element', () => {
const { container } = render(
<table>
<StyledHead />
</table>
);

expect(container.firstChild!.childNodes[0].nodeName).toBe('THEAD');
});

it('renders sticky head styles', () => {
const { getByTestId } = render(
<table>
<StyledHead isSticky data-test-id="head">
<StyledHeaderRow data-test-id="header" />
</StyledHead>
</table>
);

expect(getByTestId('head')).toHaveStyleRule('position', 'sticky');
expect(getByTestId('head')).toHaveStyleRule('border-bottom-color', 'transparent', {
modifier: `& > ${StyledHeaderRow}:last-child`
});
});
});
33 changes: 30 additions & 3 deletions packages/tables/src/styled/StyledHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,42 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import styled from 'styled-components';
import { retrieveComponentStyles, DEFAULT_THEME } from '@zendeskgarden/react-theming';
import styled, { css, DefaultTheme, ThemeProps } from 'styled-components';
import { retrieveComponentStyles, DEFAULT_THEME, getColor } from '@zendeskgarden/react-theming';
import { StyledHeaderRow } from './StyledHeaderRow';

const COMPONENT_ID = 'tables.head';

interface IStyledHeadProps {
isSticky?: boolean;
}

/*
* 1. Prevent <Checkbox> or <OverflowButton> from leaking over the sticky header
* 2. Replace header row border with a box-shadow that maintains position
*/
const stickyStyles = (props: ThemeProps<DefaultTheme>) => {
const borderColor = getColor('neutralHue', 300, props.theme);

return css`
position: sticky;
top: 0;
z-index: 1; /* [1] */
box-shadow: inset 0 -${props.theme.borderWidths.sm} 0 ${borderColor}; /* [2] */
background-color: ${props.theme.colors.background};

& > ${StyledHeaderRow}:last-child {
border-bottom-color: transparent; /* [2] */
}
`;
};

export const StyledHead = styled.thead.attrs({
'data-garden-id': COMPONENT_ID,
'data-garden-version': PACKAGE_VERSION
})`
})<IStyledHeadProps>`
${props => props.isSticky && stickyStyles(props)}

${props => retrieveComponentStyles(COMPONENT_ID, props)};
`;

Expand Down
5 changes: 5 additions & 0 deletions packages/tables/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface ITableProps extends TableHTMLAttributes<HTMLTableElement> {
isReadOnly?: boolean;
}

export interface IHeadProps extends HTMLAttributes<HTMLTableSectionElement> {
/** Applies sticky header styling */
isSticky?: boolean;
}

export interface IRowProps extends HTMLAttributes<HTMLTableRowElement> {
/** Applies striped styling */
isStriped?: boolean;
Expand Down