Skip to content

Commit

Permalink
fix stylesheet ordering (#200)
Browse files Browse the repository at this point in the history
* fix stylesheet ordering

* spell correct

* Create metal-carrots-lie.md

* fix filbert-js/core version resolutions
  • Loading branch information
kuldeepkeshwar authored Dec 21, 2020
1 parent 4ecefe3 commit 5ba6c20
Show file tree
Hide file tree
Showing 18 changed files with 720 additions and 735 deletions.
11 changes: 11 additions & 0 deletions .changeset/metal-carrots-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@filbert-js/browser-stylesheet": patch
"@filbert-js/core": patch
"@filbert-js/css-ast": patch
"@filbert-js/server-stylesheet": patch
"@filbert-js/stylesheet": patch
"@filbert-js/theming": patch
"@filbert-js/website": patch
---

fix stylesheet ordering
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"resolutions": {
"**/react": "16.12.0",
"**/react-dom": "16.12.0",
"**/@filbert-js/core": "^0.0.12",
"**/@filbert-js/core": "^0.0.13",
"**/@filbert-js/browser-stylesheet": "^0.0.9",
"**/@filbert-js/style-sheet-context": "^0.0.9",
"**/@filbert-js/css-ast":"^0.0.7",
Expand All @@ -20,7 +20,7 @@
},
"scripts": {
"develop": "lerna run --stream --parallel --ignore @filbert-js/examples-* --ignore benchmarks-* develop",
"postinstall": "lerna bootstrap --force-local && yarn build-packages",
"postinstall": "yarn build-packages",
"build": "lerna run build",
"build-packages": "lerna run --ignore @filbert-js/website --ignore benchmarks-* --ignore @filbert-js/examples-* build",
"develop-packages": "lerna run --parallel --stream --ignore @filbert-js/website --ignore benchmarks-* --ignore @filbert-js/examples-* develop",
Expand Down
15 changes: 15 additions & 0 deletions packages/browser-stylesheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ function overrides(el) {
el.getChildById = function getChildById(id) {
return document.getElementById(id);
};
el.isBeforeChild = function (node1, node2) {
const children = this.children;
let a = -1,
b = -1;
for (let index = 0; index < children.length; index++) {
const element = children[index];
if (element === node1) {
b = index;
}
if (element === node2) {
a = index;
}
}
return a < b;
};
return el;
}
const init = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { styled, Global } from '@filbert-js/core';

const Button = styled('button')`
background: pink;
border: solid 1px grey;
border: solid 1px gray;
`;

render(
Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export function styled(Component, options = {}) {

const className = obj.toString();

useStylesheet(keyframes, className, styles, props[SOURCE_ORDER], S.label);
useStylesheet(
keyframes,
className,
styles,
props[SOURCE_ORDER],
ForwardRef.label,
);
const {
className: passedClassName = '',
[SOURCE_ORDER]: sourceOrder,
Expand All @@ -36,20 +42,24 @@ export function styled(Component, options = {}) {

const finalProps = Object.assign(
{
className: [S.label, className, passedClassName].join(' ').trim(),
className: [ForwardRef.label, className, passedClassName]
.join(' ')
.trim(),
[SOURCE_ORDER]: Component[IS_STYLED_COMPONENT]
? className
: undefined,
ref,
},
_props,
);

return React.createElement(as || Component, finalProps, children);
}
S[IS_STYLED_COMPONENT] = true;
S.label = options.label ? options.label : `${LABEL_PREFIX}${id}`;
const ForwardRef = React.forwardRef(S);
ForwardRef[IS_STYLED_COMPONENT] = true;
ForwardRef.label = options.label ? options.label : `${LABEL_PREFIX}${id}`;

id++;
return React.forwardRef(S);
return ForwardRef;
};
}
6 changes: 3 additions & 3 deletions packages/css-ast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ yarn add @filbert-js/css-ast
import { toAST } from '@filbert-js/css-ast';

const css = `
color: grey;
color: gray;
button {
margin: 0 1rem;
background: #1f368f;
Expand All @@ -34,11 +34,11 @@ children: Array[2]
rules: Array[1]
0: Object
name: "color"
value: "grey"
value: "gray"
start: 0
end: 168
raw: "
color: grey;
color: gray;
button {
margin: 0 1rem;
background: #1f368f;
Expand Down
19 changes: 17 additions & 2 deletions packages/server-stylesheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ class Tag {
}
return undefined;
}
isBeforeChild(node1, node2) {
const children = this._children;
let a = -1,
b = -1;
for (let index = 0; index < children.length; index++) {
const element = children[index];
if (element === node1) {
b = index;
}
if (element === node2) {
a = index;
}
}
return a < b;
}
insertBefore(el, after) {
const index = this._children.findIndex((item) => item === after);
this._children.splice(index, 0, el);
Expand Down Expand Up @@ -110,12 +125,12 @@ export const createStylesheet = (options = {}) => {
...options,
});
const _getStyles = sheet.getStyles;
sheet.getStyles = function() {
sheet.getStyles = function () {
const { root } = _getStyles.call(this);
return root.toString();
};

sheet.getReactElements = function() {
sheet.getReactElements = function () {
const { root } = _getStyles.call(this);
return root.toReactElements();
};
Expand Down
9 changes: 5 additions & 4 deletions packages/stylesheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function StyleSheet({
const styles = cssParser({ css, namespace: `.${id}` });
el.append(styles);
_css[TYPES_CSS][id] = styles;

// ensure source order
if (sourceOrder) {
root.insertBefore(el, root.getChildById(sourceOrder));
Expand All @@ -43,9 +42,11 @@ export function StyleSheet({
}
} else {
// ensure source order
if (sourceOrder) {
const el = root.getChildById(id);
root.insertBefore(el, root.getChildById(sourceOrder));
const el = root.getChildById(id);
const elBefore = root.getChildById(sourceOrder);
// check before changing element order
if (sourceOrder && root.isBeforeChild(el, elBefore)) {
root.insertBefore(el, elBefore);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/theming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { styled } from '@filbert-js/core';
import { ThemeProvider } from '@filbert-js/theming';
const Button = styled('button')`
background: ${({ theme }) => theme.colors.brand};
border: solid 1px grey;
border: solid 1px gray;
`;
const theme = {
colors: {
Expand Down
6 changes: 3 additions & 3 deletions website/docs/CSS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { css, jsx } from '@filbert-js/core';

const styles = css`
background: pink;
border: solid 1px grey;
border: solid 1px gray;
`;

render(<button css={styles}>This is a Button component.</button>);
Expand All @@ -21,7 +21,7 @@ import { css } from '@filbert-js/macro';

const styles = css`
background: pink;
border: solid 1px grey;
border: solid 1px gray;
`;

render(<button css={styles}>This is a Button component.</button>);
Expand All @@ -35,7 +35,7 @@ import { css } from '@filbert-js/core';

const styles = css`
background: pink;
border: solid 1px grey;
border: solid 1px gray;
`;

render(<button css={styles}>This is a Button component.</button>);
Expand Down
4 changes: 2 additions & 2 deletions website/docs/component-selector.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Button = styled('button')`
outline: none;
`;
const Paragraph = styled('p')`
color: grey;
color: gray;
${Button} {
color: #1f368f;
background: white;
Expand All @@ -21,7 +21,7 @@ render(
<React.Fragment>
<Button>A button</Button>
<Paragraph>
I'm grey!!
I'm gray!!
<Button>I'm inside paragraph</Button>
</Paragraph>
</React.Fragment>,
Expand Down
2 changes: 1 addition & 1 deletion website/docs/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { styled } from '@filbert-js/core';
const Base = ({ className }) => <div className={className}>{className}</div>;

const Box = styled(Base)`
background: grey;
background: gray;
color: white;
border: solid 1px gray;
padding: 0.5rem;
Expand Down
8 changes: 4 additions & 4 deletions website/docs/nested-selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react';
import { styled } from '@filbert-js/core';

const Paragraph = styled('p')`
color: grey;
color: gray;
button {
margin: 0 1rem;
background: #1f368f;
Expand All @@ -15,7 +15,7 @@ const Paragraph = styled('p')`

render(
<Paragraph>
I'm grey!!
I'm gray!!
<button>A button</button>
</Paragraph>,
);
Expand All @@ -28,7 +28,7 @@ import React from 'react';
import { styled } from '@filbert-js/core';
const Paragraph = styled('p')`
color: grey;
color: gray;
header & {
color: red;
}
Expand All @@ -39,7 +39,7 @@ render(
<header>
<Paragraph>I'm red !!</Paragraph>
</header>
<Paragraph>I'm grey !!</Paragraph>
<Paragraph>I'm gray !!</Paragraph>
</div>,
);
```
9 changes: 7 additions & 2 deletions website/docs/override-component-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { styled } from '@filbert-js/core';

const Box = styled('div')`
background: grey;
background: gray;
color: white;
border: solid 1px gray;
padding: 0.5rem;
Expand All @@ -13,6 +13,10 @@ const BlueBox = styled(Box)`
background: #1f368f;
`;

const RedBox = styled(Box)`
background: red;
`;

const Container = styled('div')`
display: flex;
> * + * {
Expand All @@ -22,8 +26,9 @@ const Container = styled('div')`

render(
<Container>
<Box>I'm grey!!</Box>
<RedBox>I'm red!!</RedBox>
<BlueBox>I'm blue!!</BlueBox>
<Box>I'm gray!!</Box>
</Container>,
);
```
2 changes: 1 addition & 1 deletion website/docs/styled-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { styled } from '@filbert-js/core';

const Button = styled('button')`
background: pink;
border: solid 1px grey;
border: solid 1px gray;
`;

render(<Button>This is a Button component.</Button>);
Expand Down
2 changes: 1 addition & 1 deletion website/docs/theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { styled } from '@filbert-js/core';
import { ThemeProvider } from '@filbert-js/theming';
const Button = styled('button')`
background: ${({ theme }) => theme.colors.brand};
border: solid 1px grey;
border: solid 1px gray;
`;
const theme = {
colors: {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/with-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { styled } from '@filbert-js/core';
const Button = styled('button')`
background: ${({ primary }) => (primary ? '#1f368f' : 'white')};
color: ${({ primary }) => (primary ? 'white' : '#1f368f')};
border: solid 1px grey;
border: solid 1px gray;
`;
const Container = styled('div')`
display: flex;
Expand Down
Loading

1 comment on commit 5ba6c20

@vercel
Copy link

@vercel vercel bot commented on 5ba6c20 Dec 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.