-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
340 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import React from 'react' | ||
|
||
export default () => <div /> | ||
export default () => <div className="bb b--muted-3" /> |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import React from 'react' | ||
import StickyRows from './StickyRows' | ||
|
||
const Layout = ({ children }) => ( | ||
<React.Fragment> | ||
{children} | ||
</React.Fragment> | ||
) | ||
const Layout = ({ children }) => { | ||
return ( | ||
<StickyRows> | ||
{children} | ||
</StickyRows> | ||
) | ||
} | ||
|
||
export default Layout |
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,33 @@ | ||
import React, { FunctionComponent } from 'react' | ||
import ReactResizeDetector from 'react-resize-detector' | ||
|
||
interface Props { | ||
sticky?: boolean | ||
offset?: number | ||
onResize?(height?: number): void | ||
} | ||
|
||
const StickyRow: FunctionComponent<Props> = ({ | ||
children, | ||
sticky, | ||
offset = 0, | ||
onResize, | ||
}) => ( | ||
<div | ||
style={sticky ? { | ||
position: 'sticky', | ||
top: offset, | ||
zIndex: 999, | ||
} : {}}> | ||
|
||
{children} | ||
|
||
{sticky && ( | ||
<ReactResizeDetector | ||
handleHeight | ||
onResize={(width, height) => onResize && onResize(height)}/> | ||
)} | ||
</div> | ||
) | ||
|
||
export default StickyRow |
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 @@ | ||
import React, { FunctionComponent, ReactElement } from 'react' | ||
import useCumulativeHeightState from '../hooks/useCumulativeHeightState' | ||
import StickyRow from './StickyRow' | ||
|
||
interface Props { | ||
children: ReactElement | ReactElement[] | ||
} | ||
|
||
const StickyRows: FunctionComponent<Props> = ({ children }: Props) => { | ||
const { updateRowHeight, getAccumulatedHeight } = useCumulativeHeightState() | ||
|
||
return ( | ||
<React.Fragment> | ||
{React.Children.map(children, (child, index) => ( | ||
<StickyRow | ||
sticky={child.props.sticky} | ||
onResize={(height: number) => updateRowHeight({ height, index })} | ||
offset={getAccumulatedHeight(index)} | ||
> | ||
{child} | ||
</StickyRow> | ||
))} | ||
</React.Fragment> | ||
) | ||
} | ||
|
||
export default StickyRows |
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,62 @@ | ||
import React from 'react' | ||
|
||
interface State { | ||
[id: string]: number | ||
} | ||
|
||
interface UpdateAction { | ||
type: 'update' | ||
payload: { | ||
index: number | ||
height: number | ||
} | ||
} | ||
|
||
type Actions = UpdateAction | ||
|
||
const reducer = (state: State, action: Actions) => { | ||
switch(action.type) { | ||
case 'update': | ||
return { | ||
...state, | ||
[action.payload.index]: action.payload.height, | ||
} | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
const useCumulativeHeightState = () => { | ||
const [state, dispatch]: [State, React.Dispatch<UpdateAction>] = React.useReducer(reducer, {}) | ||
|
||
const updateRowHeight = ({ height, index }: { height: number, index: number }) => { | ||
if (state[index] === height) { | ||
return | ||
} | ||
|
||
dispatch({ | ||
payload: { | ||
height, | ||
index, | ||
}, | ||
type: 'update', | ||
}) | ||
} | ||
|
||
const getAccumulatedHeight = (index: number) => { | ||
const sortedIndices = Object.keys(state) | ||
.map(key => parseInt(key, 10)) | ||
.sort() | ||
|
||
const indices = sortedIndices.slice(0, sortedIndices.indexOf(index)) | ||
|
||
return indices.reduce((acc, cur) => (state[cur] || 0) + acc ,0) | ||
} | ||
|
||
return { | ||
getAccumulatedHeight, | ||
updateRowHeight, | ||
} | ||
} | ||
|
||
export default useCumulativeHeightState |
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,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"allowSyntheticDefaultImports": true, | ||
"jsx": "react", | ||
"lib": ["es2017", "dom"], | ||
"module": "es6", | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"target": "es2017", | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"types": ["node", "jest", "graphql"] | ||
}, | ||
"typeAcquisition": { | ||
"enable": false | ||
}, | ||
"include": ["./typings/*.d.ts", "./**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |
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,3 @@ | ||
{ | ||
"extends": "tslint-config-vtex" | ||
} |
Oops, something went wrong.