Skip to content

Commit

Permalink
[add] Type Echo component & animate() utility
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Mar 1, 2022
1 parent b1b3043 commit 838cd6b
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 93 deletions.
19 changes: 14 additions & 5 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ A **[React][1] advanced components library** based on [TypeScript][2] & [Bootstr
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"
href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/animate.min.css"
/>
```

Expand All @@ -27,13 +31,18 @@ A **[React][1] advanced components library** based on [TypeScript][2] & [Bootstr
3. [Icon](source/Icon.tsx)
4. [Avatar](source/Avatar.tsx)
5. [Nameplate](source/Nameplate.tsx)
6. [Filter Input](source/FilterInput/index.tsx)
7. [File Picker](source/FilePicker/index.tsx)
8. [Editor](source/Editor.tsx)
6. [TypeEcho](source/TypeEcho.tsx)
7. [Filter Input](source/FilterInput/index.tsx)
8. [File Picker](source/FilePicker/index.tsx)
9. [Editor](source/Editor.tsx)
- extra: `npm i @editorjs/editorjs react-editor-js`
9. [Editor HTML](source/EditorHTML.tsx)
10. [Editor HTML](source/EditorHTML.tsx)
- extra: `npm i editorjs-html`

## Utilities

1. [`animate()`](source/animate.ts)

## Scaffolds

1. MobX: [demo][8] & [usage][9]
Expand Down
30 changes: 12 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "idea-react",
"version": "0.9.6",
"version": "0.10.0",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "A React advanced components library based on TypeScript & Bootstrap, built by idea2app remote developers team.",
Expand All @@ -26,27 +26,27 @@
"classnames": "^2.3.1",
"lodash": "^4.17.21",
"react": "17.0.2",
"react-bootstrap": "^2.0.4",
"react-bootstrap": "^2.1.2",
"react-dom": "17.0.2",
"web-utility": "^2.9.6"
"web-utility": "^3.4.2"
},
"peerDependencies": {
"@editorjs/editorjs": "^2.22.2",
"editorjs-html": "^3.4.2",
"react-editor-js": "^2.0.3"
"@editorjs/editorjs": "^2.0.0",
"editorjs-html": "^3.0.0",
"react-editor-js": "^2.0.0"
},
"devDependencies": {
"@editorjs/editorjs": "^2.22.2",
"@types/lodash": "^4.14.178",
"@editorjs/editorjs": "^2.23.2",
"@types/lodash": "^4.14.179",
"husky": "^7.0.4",
"less": "^4.1.2",
"lint-staged": "^12.1.4",
"lint-staged": "^12.3.4",
"microbundle": "^0.14.2",
"open-cli": "^7.0.1",
"prettier": "^2.5.1",
"typedoc": "^0.22.10",
"typedoc-plugin-mdn-links": "^1.0.4",
"typescript": "~4.5.4"
"typedoc": "^0.22.12",
"typedoc-plugin-mdn-links": "^1.0.5",
"typescript": "~4.3.5"
},
"prettier": {
"singleQuote": true,
Expand All @@ -57,12 +57,6 @@
"lint-staged": {
"*.{md,less,json,yml,ts,tsx}": "prettier --write"
},
"browserslist": "> 0.5%, last 2 versions, not dead",
"targets": {
"main": {
"optimize": true
}
},
"scripts": {
"prepare": "husky install",
"test": "lint-staged",
Expand Down
39 changes: 39 additions & 0 deletions source/TypeEcho.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { sleep } from 'web-utility';
import classNames from 'classnames';
import React, { PropsWithoutRef, RefCallback, PureComponent } from 'react';

export type TypeEchoProps = PropsWithoutRef<{
className?: string;
text: string;
intervals?: number;
}>;
type State = { echoed: string };

export class TypeEcho extends PureComponent<TypeEchoProps, State> {
state: Readonly<State> = { echoed: '' };

init: RefCallback<HTMLPreElement> = node =>
node &&
new IntersectionObserver(async ([{ isIntersecting }]) => {
if (!isIntersecting) return;

const { text, intervals = 150 } = this.props;

for (const char of text) {
await sleep(intervals / 1000);

this.setState({ echoed: this.state.echoed + char });
}
}).observe(node);

render() {
const { className, text } = this.props,
{ echoed } = this.state;

return (
<pre className={classNames('text-wrap', className)} ref={this.init}>
{echoed + (echoed === text ? '' : '_')}
</pre>
);
}
}
20 changes: 20 additions & 0 deletions source/animate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RefCallback } from 'react';

export function animate(name: string): RefCallback<Element> {
return element =>
element &&
new IntersectionObserver(([{ isIntersecting, target }], observer) => {
if (!isIntersecting) return;

const classes = ['animate__animated', `animate__${name}`];

target.classList.add(...classes);

(target as HTMLElement).addEventListener(
'animationend',
() => target.classList.remove(...classes),
{ once: true }
);
observer.disconnect();
}).observe(element);
}
3 changes: 3 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ export * from './PaginationBar';
export * from './Icon';
export * from './Avatar';
export * from './Nameplate';
export * from './TypeEcho';
export * from './FilterInput';
export * from './FilePicker';
export * from './Editor';
export * from './EditorHTML';

export * from './animate';
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"target": "ES5",
"esModuleInterop": true,
"jsx": "react",
"skipLibCheck": true,
Expand Down
Loading

0 comments on commit 838cd6b

Please sign in to comment.