├── .eslintrc.yml
├── .github
└── workflows
│ └── node.js.yml
├── .gitignore
├── LICENSE
├── README.md
├── assets
├── intro.gif
└── logo.png
├── babel.config.js
├── docs
├── api-docs
│ ├── .nojekyll
│ ├── assets
│ │ ├── highlight.css
│ │ ├── main.js
│ │ ├── navigation.js
│ │ ├── search.js
│ │ └── style.css
│ ├── functions
│ │ └── useGlitch.html
│ ├── index.html
│ ├── modules.html
│ └── types
│ │ └── GlitchHandle.html
├── assets
│ ├── index-1e438818.css
│ └── index-79941713.js
├── coverage
│ ├── clover.xml
│ ├── coverage-final.json
│ ├── lcov-report
│ │ ├── base.css
│ │ ├── block-navigation.js
│ │ ├── favicon.png
│ │ ├── index.html
│ │ ├── index.ts.html
│ │ ├── prettify.css
│ │ ├── prettify.js
│ │ ├── sort-arrow-sprite.png
│ │ └── sorter.js
│ └── lcov.info
└── index.html
├── index.html
├── jest.config.ts
├── package-lock.json
├── package.json
├── src
├── docs
│ ├── App.tsx
│ └── index.css
├── lib
│ └── index.ts
└── main.tsx
├── test
└── useGlitch.test.tsx
├── tsconfig.json
├── tsconfig.node.json
├── typedoc.json
├── vite-docs.config.ts
├── vite-env.d.ts
└── vite.config.ts
/.eslintrc.yml:
--------------------------------------------------------------------------------
1 | env:
2 | browser: true
3 | es2021: true
4 | extends:
5 | - eslint:recommended
6 | - plugin:react/recommended
7 | - plugin:@typescript-eslint/recommended
8 | overrides: []
9 | parser: '@typescript-eslint/parser'
10 | parserOptions:
11 | ecmaVersion: latest
12 | sourceType: module
13 | plugins:
14 | - react
15 | - '@typescript-eslint'
16 | rules:
17 | indent:
18 | - error
19 | - 4
20 | linebreak-style:
21 | - error
22 | - unix
23 | quotes:
24 | - error
25 | - single
26 | semi:
27 | - error
28 | - always
29 |
--------------------------------------------------------------------------------
/.github/workflows/node.js.yml:
--------------------------------------------------------------------------------
1 | # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3 |
4 | name: Node.js CI
5 |
6 | on:
7 | push:
8 | branches: [ "master" ]
9 | pull_request:
10 | branches: [ "master" ]
11 |
12 | jobs:
13 | build:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | strategy:
18 | matrix:
19 | node-version: [16.x, 18.x, 20.x]
20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21 |
22 | steps:
23 | - uses: actions/checkout@v3
24 | - name: Use Node.js ${{ matrix.node-version }}
25 | uses: actions/setup-node@v3
26 | with:
27 | node-version: ${{ matrix.node-version }}
28 | cache: 'npm'
29 | - run: npm ci
30 | - run: npm run build --if-present
31 | - run: npm test
32 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | pnpm-debug.log*
8 | lerna-debug.log*
9 |
10 | node_modules
11 | dist
12 | dist-ssr
13 | *.local
14 | /coverage
15 |
16 | # Lib
17 | /lib
18 | /types
19 |
20 | # Editor directories and files
21 | .vscode/*
22 | !.vscode/extensions.json
23 | .idea
24 | .DS_Store
25 | *.suo
26 | *.ntvs*
27 | *.njsproj
28 | *.sln
29 | *.sw?
30 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Benjamin Raymond
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.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React PowerGlitch
2 |
3 |
4 |
5 | This React library is a wrapper around [PowerGlitch](https://github.com/7PH/powerglitch). PowerGlitch is a standalone library with no external dependencies. It leverages CSS animations to glitch anything on the web, without using a canvas. It weights less than 2kb minified and gzipped.
6 |
7 | - [Original project repository and documentation](https://github.com/7PH/powerglitch)
8 | - [Code coverage report for react-powerglitch](https://7ph.github.io/react-powerglitch/coverage/lcov-report/)
9 | - [API reference for react-powerglitch](https://7ph.github.io/react-powerglitch/api-docs/)
10 |
11 | # Getting started
12 |
13 |
14 | Install 15 | → Glitch 16 | → Customize 17 | → Controls 18 | / TypeScript 19 |
20 | 21 | ## Install 22 | 23 | 1. Install `react-powerglitch` using a package manager 24 | ```bash 25 | npm i --save react-powerglitch 26 | # or 27 | yarn add react-powerglitch 28 | ``` 29 | 30 | 2. Import the `useGlitch` hook from `react-powerglitch` anytime you want to use it. 31 | ```js 32 | import { useGlitch } from 'react-powerglitch' 33 | ``` 34 | 35 | ## Glitch 36 | 37 | In order to glitch something, call `useGlitch()` and store its result in a variable. 38 | ```jsx 39 | function App() { 40 | const glitch = useGlitch(); 41 | 42 | return ( 43 |Optional
userOptions: RecursivePartial<PowerGlitchOptions>Options given to PowerGlitch
30 |A glitch handle with glitch controls, a function to update the options and
33 | 34 |Generated using TypeDoc
This React library is a wrapper around PowerGlitch. PowerGlitch is a standalone library with no external dependencies. It leverages CSS animations to glitch anything on the web, without using a canvas. It weights less than 2kb minified and gzipped.
18 |24 | Install 25 | → Glitch 26 | → Customize 27 | → Controls 28 | / TypeScript 29 |
30 | 31 |Install react-powerglitch
using a package manager
npm i --save react-powerglitch
# or
yarn add react-powerglitch
34 |
35 | Import the useGlitch
hook from react-powerglitch
anytime you want to use it.
import { useGlitch } from 'react-powerglitch'
38 |
39 | In order to glitch something, call useGlitch()
and store its result in a variable.
function App() {
const glitch = useGlitch();
return (
<h1>react-powerglitch <span ref={glitch.ref}>🌎</span></h1>
);
}
43 |
44 | One limitation, when having the createContainers
option set to true (which is the default), to not place the glitched element as the direct child of a component or a conditional rendering block. E.g. this will not work:
<>
{/* Will not work */}
{condition &&
<span ref={glitch.ref}>🌎</span>
}
</>
46 |
47 | Instead, wrap the glitched element with a container:
48 |<>
{/* Will work */}
{condition &&
<div>
<span ref={glitch.ref}>🌎</span>
</div>
}
</>
49 |
50 | You can pass options to customize the glitch effect to useGlitch
:
function App() {
const glitch = useGlitch({ glitchTimeSpan: false });
return (
<h1>react-powerglitch <span ref={glitch.ref}>🌎</span></h1>
);
}
52 |
53 | The options
props accept any value defined in the original PowerGlitch library.
Take a look at the playground to visually find out the best glitch options for your element.
55 |The useGlitch
hook returns an object containing:
ref
: A function ref which you should use on the element you want to glitch, as shown in previous sections.startGlitch
: Glitch control functions to start the glitch animation.stopGlitch
: Glitch control functions to stop the glitch animation.setOptions
: A function to change the glitch options. This will update the glitched element with the new options.Here is an example:
63 |function App() {
const glitch = useGlitch({ glitchTimeSpan: false });
return (
<>
<div>
<h1 ref={glitch.ref}>react-powerglitch 🌎</h1>
</div>
<button onClick={glitch.startGlitch}>
Start
</button>
<button onClick={glitch.stopGlitch}>
Stop
</button>
</>
);
}
64 |
65 | The type GlitchHandle
represents the return type of the useGlitch
hook.
Your IDE should automatically identify the return type of useGlitch
to be GlitchHandle
and assign it to any variable you assign useGlitch()
to. In case you want to statically use it, import GlitchHandle
from react-powerglitch
.
import { useGlitch, GlitchHandle } from 'react-powerglitch';
function App() {
const glitch: GlitchHandle = useGlitch({ glitchTimeSpan: false });
return (
<>
<div>
<h1 ref={glitch.ref}>react-powerglitch 🌎</h1>
</div>
<button onClick={glitch.startGlitch}>
Start
</button>
<button onClick={glitch.stopGlitch}>
Stop
</button>
</>
);
}
68 |
69 | Generated using TypeDoc
Generated using TypeDoc
Handle to control the glitch once useGlitch is called.
20 |Function to use as ref for the element to glitch.
32 |Glitch control to start the glitch animation.
48 |Glitch control to stop the glitch animation.
59 |Change the glitch options.
70 |Generated using TypeDoc
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 | 58 |File | 70 |71 | | Statements | 72 |73 | | Branches | 74 |75 | | Functions | 76 |77 | | Lines | 78 |79 | |
---|---|---|---|---|---|---|---|---|---|
index.ts | 83 |
84 |
85 | |
86 | 100% | 87 |19/19 | 88 |100% | 89 |1/1 | 90 |100% | 91 |8/8 | 92 |100% | 93 |13/13 | 94 |
55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |
57 | 58 |1 67 | 2 68 | 3 69 | 4 70 | 5 71 | 6 72 | 7 73 | 8 74 | 9 75 | 10 76 | 11 77 | 12 78 | 13 79 | 14 80 | 15 81 | 16 82 | 17 83 | 18 84 | 19 85 | 20 86 | 21 87 | 22 88 | 23 89 | 24 90 | 25 91 | 26 92 | 27 93 | 28 94 | 29 95 | 30 96 | 31 97 | 32 98 | 33 99 | 34 100 | 35 101 | 36 102 | 37 103 | 38 104 | 39 105 | 40 106 | 41 107 | 42 108 | 43 109 | 44 110 | 45 111 | 46 112 | 47 113 | 48 114 | 49 115 | 50 116 | 51 117 | 52 118 | 53 119 | 54 120 | 55 121 | 56 122 | 57 123 | 58 124 | 59 125 | 60 126 | 61 127 | 62 128 | 63 129 | 64 130 | 65 131 | 66 132 | 67 133 | 68 134 | 69 135 | 70 136 | 71 137 | 72 | 1x 138 | 1x 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 1x 172 | 173 | 174 | 175 | 5x 176 | 177 | 178 | 179 | 180 | 5x 181 | 5x 182 | 183 | 184 | 185 | 186 | 187 | 5x 188 | 189 | 190 | 4x 191 | 2x 192 | 193 | 194 | 195 | 196 | 2x 197 | 2x 198 | 2x 199 | 200 | 201 | 5x 202 | 203 | 204 | 205 | 206 | 207 | 208 | | import { useCallback, useState } from 'react'; 209 | import { PowerGlitch, PowerGlitchOptions, RecursivePartial } from 'powerglitch'; 210 | 211 | 212 | /** 213 | * Handle to control the glitch once useGlitch is called. 214 | */ 215 | export type GlitchHandle = { 216 | /** 217 | * Function to use as ref for the element to glitch. 218 | */ 219 | ref: (node: HTMLElement | null) => void, 220 | 221 | /** 222 | * Glitch control to start the glitch animation. 223 | */ 224 | startGlitch: () => void, 225 | 226 | /** 227 | * Glitch control to stop the glitch animation. 228 | */ 229 | stopGlitch: () => void, 230 | 231 | /** 232 | * Change the glitch options. 233 | */ 234 | setOptions: (options: RecursivePartial<PowerGlitchOptions>) => void, 235 | }; 236 | 237 | /** 238 | * Hook to glitch one element. 239 | * @param userOptions Options given to PowerGlitch 240 | * @returns A glitch handle with glitch controls, a function to update the options and 241 | */ 242 | export function useGlitch(userOptions?: RecursivePartial<PowerGlitchOptions>): GlitchHandle { 243 | /** 244 | * Copy the options into a state object to avoid unecessary re-renders if the client is re-creating an options object in the main block. 245 | */ 246 | const [options, setOptions] = useState(userOptions); 247 | 248 | /** 249 | * Placeholder functions to start/stop the glitch, set after actually glitching the element. 250 | */ 251 | const [startGlitch, setStartGlitch] = useState<(() => void)>(() => () => void 0); 252 | const [stopGlitch, setStopGlitch] = useState<(() => void)>(() => () => void 0); 253 | 254 | /** 255 | * Will run each time the ref to the node to glitch changes. 256 | * E.g. after mount or when added/removed due to conditional rendering. 257 | */ 258 | const ref = useCallback((node: HTMLElement | null) => { 259 | // If glitching an element inside a conditional render, 260 | // `node` might not exist at some point, in which case we have nothing to glitch. 261 | if (! node) { 262 | return; 263 | } 264 | 265 | // When/if node is visible, glitch it 266 | // Because of useCallback, we should not glitch an already glitched element, even though the underlying library supports it. 267 | const result = PowerGlitch.glitch(node, options); 268 | setStartGlitch(() => result.startGlitch); 269 | setStopGlitch(() => result.stopGlitch); 270 | }, [options]); 271 | 272 | return { 273 | ref, 274 | startGlitch, 275 | stopGlitch, 276 | setOptions, 277 | }; 278 | } 279 | |
Hook to glitch one element.
23 |