Skip to content

Commit 0718802

Browse files
committed
feat: added new package for utils
1 parent 65cd17d commit 0718802

20 files changed

+293
-62
lines changed

components/pagers/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@
5555
"dependencies": {
5656
"@stickyroll/context": "^1.0.0",
5757
"@stickyroll/themes": "^1.0.0",
58+
"@stickyroll/utils": "^1.0.0",
5859
"@types/react": "^16.7.13",
5960
"@types/styled-components": "^4.1.3",
61+
"ava": "1.0.0-rc.2",
6062
"react": "^16.6.3",
6163
"styled-components": "^4.1.2",
6264
"ts-lib": "0.0.5"

components/pagers/src/pagers.tsx

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
import React from "react";
2-
import styled, {css, StyledComponent} from "styled-components";
3-
import {ScrollConsumer} from "@stickyroll/context";
4-
import {light, ITheme} from "@stickyroll/themes";
5-
6-
/**
7-
*
8-
* @param {any} value
9-
* @param {string} type
10-
* @throws {?TypeError}
11-
*/
12-
export const assert = (value: any, type: string): void => {
13-
const valueType = typeof value;
14-
if (valueType !== type) {
15-
throw new TypeError(`Expected "${type}" but "${valueType}" was received`);
16-
}
17-
};
2+
import styled, { css, StyledComponent } from "styled-components";
3+
import { ScrollConsumer } from "@stickyroll/context";
4+
import { ITheme, light } from "@stickyroll/themes";
5+
import { scrollTo } from "@stickyroll/utils";
6+
import { assert } from "@stickyroll/utils";
187

198
/**
209
* @extends React.HTMLAttributes<HTMLAnchorElement>
@@ -221,47 +210,6 @@ export const SkipLink: StyledComponent<"a", {}> = styled.a`
221210
}
222211
`;
223212

224-
/**
225-
* @typedef {object} IScrollOptions
226-
* @property {boolean} noFocus
227-
* @property {boolean} noHash
228-
*/
229-
export interface IScrollToOptions {
230-
noFocus?: boolean;
231-
noHash?: boolean;
232-
}
233-
234-
/**
235-
* @param {string} hash
236-
* @param {HTMLElement} target
237-
* @param {IScrollToOptions} [options={}]
238-
* @return {void}
239-
*/
240-
const scrollTo = (hash: string, target: HTMLElement, options: IScrollToOptions = {}): void => {
241-
if (!options.noHash) {
242-
window.location.hash = hash;
243-
}
244-
const el = document.getElementById(hash);
245-
if (!options.noFocus) {
246-
target.focus();
247-
}
248-
249-
// Attempted to implement smooth scrolling if the page changes by one position.
250-
// The page jumps in several state changes
251-
// @todo Fix unless a browser bug exists.
252-
// const index = parseInt(hash.split("/").reverse()[0], 10) - 1;
253-
// const diff = Math.abs(index - page);
254-
// document.documentElement.style["scroll-behavior"] = diff > 1 ? "auto" : "smooth";
255-
256-
el.scrollIntoView(true);
257-
258-
// Optionally if Element.scrollIntoView does not return the expected result.
259-
// const {top: tEl} = el.getBoundingClientRect();
260-
// const {top: tBody} = document.body.getBoundingClientRect();
261-
// const offset = tEl - tBody;
262-
// window.scrollTo(0, offset);
263-
};
264-
265213
/**
266214
* @typedef {object} ISkipBaseProps
267215
* @property {string} prefix
@@ -363,11 +311,6 @@ export const PagerBase: React.FunctionComponent<IPagerBaseProps> = props => {
363311
);
364312
};
365313

366-
/**
367-
* Make all properties in T optional
368-
*/
369-
type PartialProps<T> = {[P in keyof T]?: T[P]};
370-
371314
/**
372315
* @extends Partial<IPagerBaseProps>
373316
* @typedef {object} IPagersProps

packages/utils/.babelrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
"extends": require.resolve("imhotep/config/.babelrc")
3+
}

packages/utils/.editorconfig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
end_of_line = lf
7+
trim_trailing_spaces = true
8+
insert_final_newline = true
9+
10+
# trailing spaces in markdown indicate word wrap
11+
[*.md]
12+
trim_trailing_spaces = false
13+
max_line_length = 100
14+
15+
16+
# Set default charset
17+
[*.{ts,tsx,js,jsx}]
18+
charset = utf-8
19+
max_line_length = 100
20+
21+
# Set indents
22+
[*.{yml,json}]
23+
max_line_length = 100
24+
indent_size = 2
25+
26+
# Set spaces
27+
[*.{yml}]
28+
max_line_length = 100
29+
indent_style = space

packages/utils/.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

packages/utils/.stylelintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"processors": ["stylelint-processor-styled-components"],
3+
"extends": ["imhotep/config/.stylelintrc", "stylelint-config-styled-components"]
4+
}

packages/utils/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 stickyroll
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/utils/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Stickyroll utils
2+
3+
Utils for stickyroll.
4+
5+
```bash
6+
yarn add @stickyroll/utils
7+
```

packages/utils/ava.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
compileEnhancements: false,
3+
require: [],
4+
babel: {
5+
extensions: ["js", "jsx"]
6+
},
7+
files: ["test/**/*.js"]
8+
};

packages/utils/imhotep.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
lib: {
3+
path: "dist"
4+
},
5+
types: {
6+
path: "dist"
7+
},
8+
src: {
9+
path: "src"
10+
}
11+
};

0 commit comments

Comments
 (0)