Skip to content

Commit

Permalink
Sticky: remove deprecated dangerouslySetZIndex (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianvuerings committed Sep 17, 2020
1 parent 51ac423 commit 5d15918
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Expand Up @@ -4,6 +4,7 @@
[ignore]
<PROJECT_ROOT>/packages/gestalt-codemods/.*/__testfixtures__
<PROJECT_ROOT>/cypress
<PROJECT_ROOT>/node_modules/cypress/

[options]
esproposal.optional_chaining=enable
Expand Down
@@ -0,0 +1,50 @@
/*
* Log an error when `dangerouslySetZIndex` is specified on `<Sticky />`
*/

export default function transformer(file, api) {
const j = api.jscodeshift;
const src = j(file.source);
let localIdentifierName;

src.find(j.ImportDeclaration).forEach(path => {
const decl = path.node;
if (decl.source.value !== 'gestalt') {
return null;
}

localIdentifierName = decl.specifiers
.filter(node => node.imported.name === 'Sticky')
.map(node => node.local.name);
return null;
});

if (!localIdentifierName) {
return null;
}

src
.find(j.JSXElement)
.forEach(jsxElement => {
const { node } = jsxElement;

if (!localIdentifierName.includes(node.openingElement.name.name)) {
return null;
}

const attrs = node.openingElement.attributes;

attrs.forEach(attr => {
if (attr.name && attr.name.name === 'dangerouslySetZIndex') {
// eslint-disable-next-line no-console
console.error(
`Replace legacy dangerouslySetZIndex with zIndex on Sticky: ${file.path}:${attr.loc.start.line}:${attr.loc.start.column}`
);
}
});
return null;
})
.toSource();

return null;
}
29 changes: 29 additions & 0 deletions packages/gestalt/src/Sticky.flowtest.js
@@ -0,0 +1,29 @@
// @flow strict
import React from 'react';
import Sticky from './Sticky.js';
import { FixedZIndex } from './zIndex.js';

const ValidDefaultStickyType = <Sticky top={0}>Content</Sticky>;

const ValidZIndex = (
<Sticky top={0} zIndex={new FixedZIndex(1)}>
Content
</Sticky>
);

const InValidZIndex = (
// $FlowExpectedError[incompatible-type]
<Sticky top={0} zIndex={1}>
Content
</Sticky>
);

// $FlowExpectedError[incompatible-type]
const MissingProp = <Sticky />;

const IncompatibleLegacyZIndexProp = (
// $FlowExpectedError[incompatible-type]
<Sticky top={0} dangerouslySetZIndex={1}>
Content
</Sticky>
);
14 changes: 2 additions & 12 deletions packages/gestalt/src/Sticky.js
Expand Up @@ -22,22 +22,15 @@ type Threshold =
type Props = {|
...Threshold,
children: Node,
dangerouslySetZIndex?: {| __zIndex: number |},
height?: number,
zIndex?: Indexable,
|};

const DEFAULT_ZINDEX = new FixedZIndex(1);

export default function Sticky(props: Props): Node {
const { dangerouslySetZIndex, children, height } = props;
const zIndex =
props.zIndex ||
(dangerouslySetZIndex &&
Object.prototype.hasOwnProperty.call(dangerouslySetZIndex, '__zIndex')
? // eslint-disable-next-line no-underscore-dangle
new FixedZIndex(dangerouslySetZIndex.__zIndex)
: DEFAULT_ZINDEX);
const { children, height } = props;
const zIndex = props.zIndex || DEFAULT_ZINDEX;
const style = {
...(height !== undefined ? { height } : {}),
top: props.top != null ? props.top : undefined,
Expand All @@ -55,9 +48,6 @@ export default function Sticky(props: Props): Node {

Sticky.propTypes = {
children: PropTypes.node,
dangerouslySetZIndex: PropTypes.exact({
__zIndex: PropTypes.number,
}),
top: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
left: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
bottom: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
Expand Down

0 comments on commit 5d15918

Please sign in to comment.