├── .gitignore ├── LICENSE ├── README.md ├── example ├── .gitignore ├── README.md ├── package.json ├── public │ ├── favicon.png │ ├── global.css │ └── index.html ├── rollup.config.js ├── src │ ├── App.svelte │ └── main.ts └── tsconfig.json ├── package-lock.json ├── package.json ├── rollup.config.js ├── scripts └── preprocess.js ├── src ├── index.ts └── lib │ ├── components │ ├── Avatar.svelte │ ├── AvatarBauhaus.svelte │ ├── AvatarBeam.svelte │ ├── AvatarMarble.svelte │ ├── AvatarPixel.svelte │ ├── AvatarRing.svelte │ ├── AvatarSunset.svelte │ └── CONSTANTS.ts │ └── utils.ts ├── svelte.config.js ├── tests └── Avatar.test.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 paolotiu 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 | # Svelte-Boring-Avatars 2 | ![Screenshot 2021-09-01 100049](https://user-images.githubusercontent.com/61054234/131599513-98c3c763-9839-4539-8c83-0a3be137f59a.png) 3 | 4 | A Svelte port of [Boring Avatars](https://github.com/boringdesigners/boring-avatars). 5 | 6 | ## Demo 7 | 8 | [REPL](https://svelte.dev/repl/889946205a7542a4a6e0e1e7e98f37ba?version=4.2.9) 9 | 10 | ## Installation 11 | 12 | ```bash 13 | npm install svelte-boring-avatars 14 | 15 | # or 16 | 17 | yarn add svelte-boring-avatars 18 | ``` 19 | 20 | ## Usage 21 | 22 | ```svelte 23 | 26 | 27 | 33 | ``` 34 | 35 | ## Props 36 | 37 | | Prop | Type | Default | 38 | | ------- | -------------------------------------------------------- | --------------------------------------------------------- | 39 | | size | number | 40 | 40 | | square | boolean | false | 41 | | variant | `"bauhaus", "beam", "marble", "pixel", "ring", "sunset"` | `"marble"` | 42 | | colors | string[] | `["#92A1C6", "#146A7C", "#F0AB3D", "#C271B4", "#C20D90"]` | 43 | 44 | ## Reducing Bundle Size 45 | 46 | This library is small as it is, but if you want to shave off some more load you can import a specific variant. 47 | 48 | All the props stay the same except for the exclusion of `variant`. 49 | 50 | ```svelte 51 | 54 | 55 | 60 | ``` 61 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /public/build/ 3 | 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | *Looking for a shareable component template? Go here --> [sveltejs/component-template](https://github.com/sveltejs/component-template)* 2 | 3 | --- 4 | 5 | # svelte app 6 | 7 | This is a project template for [Svelte](https://svelte.dev) apps. It lives at https://github.com/sveltejs/template. 8 | 9 | To create a new project based on this template using [degit](https://github.com/Rich-Harris/degit): 10 | 11 | ```bash 12 | npx degit sveltejs/template svelte-app 13 | cd svelte-app 14 | ``` 15 | 16 | *Note that you will need to have [Node.js](https://nodejs.org) installed.* 17 | 18 | 19 | ## Get started 20 | 21 | Install the dependencies... 22 | 23 | ```bash 24 | cd svelte-app 25 | npm install 26 | ``` 27 | 28 | ...then start [Rollup](https://rollupjs.org): 29 | 30 | ```bash 31 | npm run dev 32 | ``` 33 | 34 | Navigate to [localhost:5000](http://localhost:5000). You should see your app running. Edit a component file in `src`, save it, and reload the page to see your changes. 35 | 36 | By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the `sirv` commands in package.json to include the option `--host 0.0.0.0`. 37 | 38 | If you're using [Visual Studio Code](https://code.visualstudio.com/) we recommend installing the official extension [Svelte for VS Code](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode). If you are using other editors you may need to install a plugin in order to get syntax highlighting and intellisense. 39 | 40 | ## Building and running in production mode 41 | 42 | To create an optimised version of the app: 43 | 44 | ```bash 45 | npm run build 46 | ``` 47 | 48 | You can run the newly built app with `npm run start`. This uses [sirv](https://github.com/lukeed/sirv), which is included in your package.json's `dependencies` so that the app will work when you deploy to platforms like [Heroku](https://heroku.com). 49 | 50 | 51 | ## Single-page app mode 52 | 53 | By default, sirv will only respond to requests that match files in `public`. This is to maximise compatibility with static fileservers, allowing you to deploy your app anywhere. 54 | 55 | If you're building a single-page app (SPA) with multiple routes, sirv needs to be able to respond to requests for *any* path. You can make it so by editing the `"start"` command in package.json: 56 | 57 | ```js 58 | "start": "sirv public --single" 59 | ``` 60 | 61 | ## Using TypeScript 62 | 63 | This template comes with a script to set up a TypeScript development environment, you can run it immediately after cloning the template with: 64 | 65 | ```bash 66 | node scripts/setupTypeScript.js 67 | ``` 68 | 69 | Or remove the script via: 70 | 71 | ```bash 72 | rm scripts/setupTypeScript.js 73 | ``` 74 | 75 | ## Deploying to the web 76 | 77 | ### With [Vercel](https://vercel.com) 78 | 79 | Install `vercel` if you haven't already: 80 | 81 | ```bash 82 | npm install -g vercel 83 | ``` 84 | 85 | Then, from within your project folder: 86 | 87 | ```bash 88 | cd public 89 | vercel deploy --name my-project 90 | ``` 91 | 92 | ### With [surge](https://surge.sh/) 93 | 94 | Install `surge` if you haven't already: 95 | 96 | ```bash 97 | npm install -g surge 98 | ``` 99 | 100 | Then, from within your project folder: 101 | 102 | ```bash 103 | npm run build 104 | surge public my-project.surge.sh 105 | ``` 106 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svelte-app", 3 | "version": "1.0.0", 4 | "scripts": { 5 | "build": "rollup -c", 6 | "dev": "rollup -c -w", 7 | "start": "sirv public", 8 | "validate": "svelte-check" 9 | }, 10 | "devDependencies": { 11 | "@rollup/plugin-commonjs": "^14.0.0", 12 | "@rollup/plugin-node-resolve": "^8.0.0", 13 | "@rollup/plugin-typescript": "^6.0.0", 14 | "@tsconfig/svelte": "^1.0.0", 15 | "rollup": "^2.3.4", 16 | "rollup-plugin-livereload": "^2.0.0", 17 | "rollup-plugin-svelte": "^6.0.0", 18 | "rollup-plugin-terser": "^7.0.0", 19 | "svelte": "^3.0.0", 20 | "svelte-check": "^1.0.0", 21 | "svelte-preprocess": "^4.0.0", 22 | "svelte-boring-avatars": "file:../..", 23 | "tslib": "^2.0.0", 24 | "typescript": "^3.9.3" 25 | }, 26 | "dependencies": { 27 | "sirv-cli": "^1.0.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /example/public/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paolotiu/svelte-boring-avatars/d85069734a0e5a8647bbec8cea31cf0373adc574/example/public/favicon.png -------------------------------------------------------------------------------- /example/public/global.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | position: relative; 3 | width: 100%; 4 | height: 100%; 5 | } 6 | 7 | body { 8 | color: #333; 9 | margin: 0; 10 | padding: 8px; 11 | box-sizing: border-box; 12 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; 13 | } 14 | 15 | a { 16 | color: rgb(0,100,200); 17 | text-decoration: none; 18 | } 19 | 20 | a:hover { 21 | text-decoration: underline; 22 | } 23 | 24 | a:visited { 25 | color: rgb(0,80,160); 26 | } 27 | 28 | label { 29 | display: block; 30 | } 31 | 32 | input, button, select, textarea { 33 | font-family: inherit; 34 | font-size: inherit; 35 | -webkit-padding: 0.4em 0; 36 | padding: 0.4em; 37 | margin: 0 0 0.5em 0; 38 | box-sizing: border-box; 39 | border: 1px solid #ccc; 40 | border-radius: 2px; 41 | } 42 | 43 | input:disabled { 44 | color: #ccc; 45 | } 46 | 47 | button { 48 | color: #333; 49 | background-color: #f4f4f4; 50 | outline: none; 51 | } 52 | 53 | button:disabled { 54 | color: #999; 55 | } 56 | 57 | button:not(:disabled):active { 58 | background-color: #ddd; 59 | } 60 | 61 | button:focus { 62 | border-color: #666; 63 | } 64 | -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Svelte app 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/rollup.config.js: -------------------------------------------------------------------------------- 1 | import svelte from 'rollup-plugin-svelte'; 2 | import resolve from '@rollup/plugin-node-resolve'; 3 | import commonjs from '@rollup/plugin-commonjs'; 4 | import livereload from 'rollup-plugin-livereload'; 5 | import { terser } from 'rollup-plugin-terser'; 6 | import sveltePreprocess from 'svelte-preprocess'; 7 | import typescript from '@rollup/plugin-typescript'; 8 | 9 | const production = !process.env.ROLLUP_WATCH; 10 | 11 | function serve() { 12 | let server; 13 | 14 | function toExit() { 15 | if (server) server.kill(0); 16 | } 17 | 18 | return { 19 | writeBundle() { 20 | if (server) return; 21 | server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], { 22 | stdio: ['ignore', 'inherit', 'inherit'], 23 | shell: true 24 | }); 25 | 26 | process.on('SIGTERM', toExit); 27 | process.on('exit', toExit); 28 | } 29 | }; 30 | } 31 | 32 | export default { 33 | input: 'src/main.ts', 34 | output: { 35 | sourcemap: true, 36 | format: 'iife', 37 | name: 'app', 38 | file: 'public/build/bundle.js' 39 | }, 40 | plugins: [ 41 | svelte({ 42 | // enable run-time checks when not in production 43 | dev: !production, 44 | // we'll extract any component CSS out into 45 | // a separate file - better for performance 46 | css: css => { 47 | css.write('bundle.css'); 48 | }, 49 | preprocess: sveltePreprocess(), 50 | }), 51 | 52 | // If you have external dependencies installed from 53 | // npm, you'll most likely need these plugins. In 54 | // some cases you'll need additional configuration - 55 | // consult the documentation for details: 56 | // https://github.com/rollup/plugins/tree/master/packages/commonjs 57 | resolve({ 58 | browser: true, 59 | dedupe: ['svelte'] 60 | }), 61 | commonjs(), 62 | typescript({ 63 | sourceMap: !production, 64 | inlineSources: !production 65 | }), 66 | 67 | // In dev mode, call `npm run start` once 68 | // the bundle has been generated 69 | !production && serve(), 70 | 71 | // Watch the `public` directory and refresh the 72 | // browser on changes when not in production 73 | !production && livereload('public'), 74 | 75 | // If we're building for production (npm run build 76 | // instead of npm run dev), minify 77 | production && terser() 78 | ], 79 | watch: { 80 | clearScreen: false 81 | } 82 | }; 83 | -------------------------------------------------------------------------------- /example/src/App.svelte: -------------------------------------------------------------------------------- 1 | 20 | 21 |
22 |
23 | 24 | 25 |
26 | 27 |
28 | 29 |
30 | 31 | 32 | 36 |
37 |
38 |
39 | 40 | 41 |
42 | 43 |
44 | 45 | 53 |
54 | 55 |
56 |
57 |

Color

58 |
59 | 60 | 61 |
62 |
63 | 64 |
65 | {#each colors as color, i} 66 |
67 |
68 |

69 | {color} 70 |

71 | 72 | 80 |
81 | {/each} 82 |
83 |
84 |
85 | 86 |
87 | 88 |
89 | 90 | 192 | -------------------------------------------------------------------------------- /example/src/main.ts: -------------------------------------------------------------------------------- 1 | import App from "./App.svelte"; 2 | 3 | const app = new App({ 4 | target: document.body, 5 | }); 6 | 7 | export default app; 8 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/svelte/tsconfig.json", 3 | "include": ["src/**/*"], 4 | "exclude": ["node_modules/*", "__sapper__/*", "public/*"] 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svelte-boring-avatars", 3 | "svelte": "dist/index.js", 4 | "exports": { 5 | ".": { 6 | "svelte": "./dist/index.js", 7 | "types": "./dist/ts/index.d.ts" 8 | } 9 | }, 10 | "module": "dist/index.mjs", 11 | "main": "dist/index.umd.js", 12 | "types": "dist/ts/index.d.ts", 13 | "author": "Paolo Tiu ", 14 | "license": "MIT", 15 | "version": "1.2.6", 16 | "sideEffects": false, 17 | "scripts": { 18 | "prebuild": "rm -rf ./dist", 19 | "build": "rollup -c", 20 | "dev": "rollup -c -w", 21 | "validate": "svelte-check", 22 | "prepublishOnly": "npm run build", 23 | "test": "jest tests" 24 | }, 25 | "files": [ 26 | "src", 27 | "dist" 28 | ], 29 | "devDependencies": { 30 | "@rollup/plugin-node-resolve": "^13.0.4", 31 | "@rollup/plugin-typescript": "^8.2.5", 32 | "@testing-library/jest-dom": "^5.14.1", 33 | "@testing-library/svelte": "^3.0.3", 34 | "@tsconfig/svelte": "^1.0.10", 35 | "@types/jest": "^27.0.1", 36 | "babel-jest": "^27.1.0", 37 | "fs-extra": "^10.0.0", 38 | "glob": "^7.1.6", 39 | "jest": "^27.1.0", 40 | "prettier": "^2.3.2", 41 | "prettier-plugin-svelte": "^2.4.0", 42 | "rollup": "^2.32.0", 43 | "rollup-plugin-execute": "^1.1.1", 44 | "rollup-plugin-filesize": "^9.0.2", 45 | "rollup-plugin-size-snapshot": "^0.12.0", 46 | "rollup-plugin-svelte": "^7.1.0", 47 | "rollup-plugin-terser": "^7.0.2", 48 | "rollup-plugin-typescript2": "^0.30.0", 49 | "rollup-plugin-visualizer": "^5.5.2", 50 | "svelte": "^3.24.0", 51 | "svelte-check": "^2.2.5", 52 | "svelte-jester": "^1.8.2", 53 | "svelte-preprocess": "^4.5.1", 54 | "ts-jest": "^27.0.5", 55 | "tslib": "^2.3.1", 56 | "typescript": "^4.3.5" 57 | }, 58 | "keywords": [ 59 | "svelte", 60 | "avatars", 61 | "boring-avatars" 62 | ], 63 | "repository": { 64 | "type": "git", 65 | "url": "https://github.com/paolotiu/svelte-boring-avatars.git" 66 | }, 67 | "jest": { 68 | "transform": { 69 | "^.+\\.svelte$": [ 70 | "svelte-jester", 71 | { 72 | "preprocess": true 73 | } 74 | ], 75 | "^.+\\.ts$": "ts-jest" 76 | }, 77 | "moduleFileExtensions": [ 78 | "js", 79 | "svelte", 80 | "ts" 81 | ] 82 | }, 83 | "setupFilesAfterEnv": [ 84 | "@testing-library/jest-dom/extend-expect" 85 | ] 86 | } 87 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import svelte from 'rollup-plugin-svelte'; 2 | import resolve from '@rollup/plugin-node-resolve'; 3 | import sveltePreprocess from 'svelte-preprocess'; 4 | import typescript from '@rollup/plugin-typescript'; 5 | import execute from 'rollup-plugin-execute'; 6 | 7 | import pkg from './package.json'; 8 | 9 | const name = pkg.name 10 | .replace(/^(@\S+\/)?(svelte-)?(\S+)/, '$3') 11 | .replace(/^\w/, (m) => m.toUpperCase()) 12 | .replace(/-\w/g, (m) => m[1].toUpperCase()); 13 | 14 | export default { 15 | input: 'src/index.ts', 16 | output: [ 17 | { 18 | file: pkg.module, 19 | format: 'es', 20 | sourcemap: true, 21 | }, 22 | { 23 | file: pkg.main, 24 | format: 'umd', 25 | name, 26 | sourcemap: true, 27 | plugins: [ 28 | // we only want to run this once, so we'll just make it part of this output's plugins 29 | execute(['tsc --outDir ./dist --declaration', 'node scripts/preprocess.js']), 30 | ], 31 | }, 32 | ], 33 | plugins: [ 34 | svelte({ 35 | preprocess: sveltePreprocess(), 36 | }), 37 | resolve(), 38 | typescript(), 39 | ], 40 | }; 41 | -------------------------------------------------------------------------------- /scripts/preprocess.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs-extra"); 2 | const glob = require("glob"); 3 | const path = require("path"); 4 | const svelte = require("svelte/compiler"); 5 | const sveltePreprocess = require("svelte-preprocess"); 6 | 7 | const basePath = path.resolve(__dirname, "../"); 8 | const srcPath = path.resolve(basePath, "src"); 9 | const distPath = path.resolve(basePath, "dist"); 10 | 11 | /** 12 | * This will process .svelte files into plain javascript so consumers do not have to setup Typescript to use this library 13 | * 14 | * Additionally, it will move the .d.ts files into /dist/ts 15 | */ 16 | async function main() { 17 | // get all .svelte files 18 | glob(path.join(srcPath, "**/*.svelte"), null, async function (err, files) { 19 | if (err) throw err; 20 | // process them 21 | await Promise.all(files.map((filePath) => preprocessSvelte(path.resolve(filePath)))); 22 | }); 23 | 24 | // move .d.ts files into /dist/ts 25 | glob(path.join(distPath, "**/*.d.ts"), null, async function (err, files) { 26 | if (err) throw err; 27 | const tsPath = path.join(distPath, "ts"); 28 | 29 | await Promise.all( 30 | files.map(async (filePath) => { 31 | filePath = path.resolve(filePath); 32 | // ignore anything in /dist/ts (could probably make a better glob pattern) 33 | if (!filePath.includes(tsPath)) { 34 | await fs.move(filePath, filePath.replace(distPath, tsPath), { 35 | overwrite: true, 36 | }); 37 | } 38 | }) 39 | ); 40 | }); 41 | } 42 | 43 | /** 44 | * Processes .svelte file and write it to /dist, also copies the original .svelte file to /dist/ts 45 | */ 46 | async function preprocessSvelte(filePath) { 47 | const srcCode = await fs.readFile(filePath, { encoding: "utf-8" }); 48 | let { code } = await svelte.preprocess( 49 | srcCode, 50 | sveltePreprocess({ 51 | // unfortunately, sourcemap on .svelte files sometimes comments out the tag 52 | // so we need to disable sourcemapping for them 53 | sourceMap: false, 54 | typescript: { 55 | compilerOptions: { 56 | sourceMap: false, 57 | }, 58 | }, 59 | }), 60 | { 61 | filename: filePath, 62 | } 63 | ); 64 | 65 | // remove lang=ts from processed .svelte files 66 | code = code.replace(/script lang="ts"/g, "script"); 67 | 68 | const relativePath = filePath.split(srcPath)[1]; 69 | const destination = path.join(distPath, filePath.split(srcPath)[1]); 70 | 71 | // write preprocessed svelte file to /dist 72 | await fs.ensureFile(destination); 73 | await fs.writeFile(destination, code, { encoding: "utf-8" }); 74 | 75 | // write the unprocessed svelte component to /dist/ts/ so we can have correct types for ts users 76 | const tsDest = path.join(distPath, "ts", relativePath); 77 | await fs.ensureFile(tsDest); 78 | await fs.writeFile(tsDest, srcCode, { 79 | encoding: "utf-8", 80 | }); 81 | } 82 | 83 | main(); 84 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import Avatar from './lib/components/Avatar.svelte'; 2 | export default Avatar; 3 | 4 | export { default as AvatarBauhaus } from './lib/components/AvatarBauhaus.svelte'; 5 | export { default as AvatarBeam } from './lib/components/AvatarBeam.svelte'; 6 | export { default as AvatarMarble } from './lib/components/AvatarMarble.svelte'; 7 | export { default as AvatarPixel } from './lib/components/AvatarPixel.svelte'; 8 | export { default as AvatarRing } from './lib/components/AvatarRing.svelte'; 9 | export { default as AvatarSunset } from './lib/components/AvatarSunset.svelte'; 10 | -------------------------------------------------------------------------------- /src/lib/components/Avatar.svelte: -------------------------------------------------------------------------------- 1 | 53 | 54 | {#key props} 55 | {#key finalVariant} 56 | 57 | {/key} 58 | {/key} 59 | -------------------------------------------------------------------------------- /src/lib/components/AvatarBauhaus.svelte: -------------------------------------------------------------------------------- 1 | 31 | 32 | 40 | 41 | 42 | 43 | 44 | 45 | 63 | 70 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /src/lib/components/AvatarBeam.svelte: -------------------------------------------------------------------------------- 1 | 47 | 48 | 56 | 57 | 58 | 59 | 60 | 61 | 82 | 95 | {#if data.isMouthOpen} 96 | 102 | {:else} 103 | 104 | {/if} 105 | 106 | 115 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /src/lib/components/AvatarMarble.svelte: -------------------------------------------------------------------------------- 1 | 33 | 34 | 42 | 43 | 44 | 45 | 46 | 47 | 66 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /src/lib/components/AvatarPixel.svelte: -------------------------------------------------------------------------------- 1 | 28 | 29 | 37 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /src/lib/components/AvatarRing.svelte: -------------------------------------------------------------------------------- 1 | 37 | 38 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /src/lib/components/AvatarSunset.svelte: -------------------------------------------------------------------------------- 1 | 31 | 32 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 56 | 57 | 58 | 59 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/lib/components/CONSTANTS.ts: -------------------------------------------------------------------------------- 1 | export const DEFAULTS = { 2 | colors: ['#92A1C6', '#146A7C', '#F0AB3D', '#C271B4', '#C20D90'], 3 | name: 'Clara Barton', 4 | square: false, 5 | size: 40, 6 | }; 7 | -------------------------------------------------------------------------------- /src/lib/utils.ts: -------------------------------------------------------------------------------- 1 | export const getNumber = (name: string) => { 2 | const charactersArray = Array.from(name); 3 | let charactersCodesSum = 0; 4 | 5 | charactersArray.forEach((charactersArrayItem) => { 6 | return (charactersCodesSum += charactersArrayItem.charCodeAt(0)); 7 | }); 8 | 9 | return charactersCodesSum; 10 | }; 11 | 12 | export const getModulus = (num: number, max: number) => { 13 | return num % max; 14 | }; 15 | 16 | export const getDigit = (number: number, ntn: number) => { 17 | return Math.floor((number / Math.pow(10, ntn)) % 10); 18 | }; 19 | 20 | export const getBoolean = (number: number, ntn: number) => { 21 | return !(getDigit(number, ntn) % 2); 22 | }; 23 | 24 | export const getAngle = (x: number, y: number) => { 25 | return (Math.atan2(y, x) * 180) / Math.PI; 26 | }; 27 | 28 | export const getUnit = (number: number, range: number, index?: number) => { 29 | let value = number % range; 30 | 31 | if (index && getDigit(number, index) % 2 === 0) { 32 | return -value; 33 | } else return value; 34 | }; 35 | 36 | export const getRandomColor = (number: number, colors: string[], range: number) => { 37 | return colors[number % range]; 38 | }; 39 | 40 | export const getContrast = (hexcolor: string) => { 41 | // If a leading # is provided, remove it 42 | if (hexcolor.slice(0, 1) === '#') { 43 | hexcolor = hexcolor.slice(1); 44 | } 45 | 46 | // Convert to RGB value 47 | var r = parseInt(hexcolor.substr(0, 2), 16); 48 | var g = parseInt(hexcolor.substr(2, 2), 16); 49 | var b = parseInt(hexcolor.substr(4, 2), 16); 50 | 51 | // Get YIQ ratio 52 | var yiq = (r * 299 + g * 587 + b * 114) / 1000; 53 | 54 | // Check contrast 55 | return yiq >= 128 ? 'black' : 'white'; 56 | }; 57 | 58 | export const getRandId = (prefix?: string) => { 59 | // doesn't use substr 60 | return (prefix || '') + Math.random().toString(36).substring(2); 61 | }; 62 | -------------------------------------------------------------------------------- /svelte.config.js: -------------------------------------------------------------------------------- 1 | const autoPreprocess = require('svelte-preprocess'); 2 | 3 | module.exports = { 4 | preprocess: autoPreprocess(), 5 | }; 6 | -------------------------------------------------------------------------------- /tests/Avatar.test.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | 5 | import '@testing-library/jest-dom'; 6 | 7 | import { getByTestId, render } from '@testing-library/svelte'; 8 | 9 | import Avatar, { 10 | AvatarBauhaus, 11 | AvatarBeam, 12 | AvatarMarble, 13 | AvatarPixel, 14 | AvatarRing, 15 | AvatarSunset, 16 | } from '../src/index'; 17 | 18 | const variantMapping = { 19 | bauhaus: AvatarBauhaus, 20 | beam: AvatarBeam, 21 | marble: AvatarMarble, 22 | pixel: AvatarPixel, 23 | ring: AvatarRing, 24 | sunset: AvatarSunset, 25 | } as const; 26 | 27 | const variants = ['bauhaus', 'beam', 'marble', 'pixel', 'ring', 'sunset'] as const; 28 | describe('Avatar component', () => { 29 | test('Creates default Avatar', async () => { 30 | const { getByTestId } = render(Avatar); 31 | 32 | expect(getByTestId('avatar-marble')).toBeInTheDocument(); 33 | }); 34 | 35 | test.each(variants)(`Creates %s variant`, (variant) => { 36 | const { getByTestId } = render(Avatar, { variant }); 37 | expect(getByTestId(`avatar-${variant}`)).toBeInTheDocument(); 38 | }); 39 | }); 40 | 41 | describe('Individual variants', () => { 42 | test.each(variants)(`Creates %s variant`, (variant) => { 43 | const { getByTestId } = render(variantMapping[variant]); 44 | expect(getByTestId(`avatar-${variant}`)).toBeInTheDocument(); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/svelte/tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": true, 5 | "strict": true 6 | }, 7 | 8 | "include": ["src/**/*"], 9 | "exclude": ["node_modules/*"] 10 | } 11 | --------------------------------------------------------------------------------