├── example ├── .npmignore ├── images │ ├── npm-icon.png │ └── github-icon.png ├── index.html ├── tsconfig.json ├── package.json ├── index.css └── index.tsx ├── scrutinizer.yml ├── .gitignore ├── example.png ├── src ├── index.tsx ├── types │ ├── UserData.ts │ └── TweetConfig.ts ├── components │ ├── tweet │ │ ├── Metadata.tsx │ │ ├── Content.tsx │ │ ├── Tweet.tsx │ │ ├── Actions.tsx │ │ ├── UserInfo.tsx │ │ ├── Impact.tsx │ │ ├── ImagesContainer.tsx │ │ └── Tweet.css │ └── icons │ │ ├── DropIcon.tsx │ │ ├── LikeIcon.tsx │ │ ├── ShareIcon.tsx │ │ ├── CommentIcon.tsx │ │ ├── LockIcon.tsx │ │ ├── RetweetIcon.tsx │ │ └── VerifiedIcon.tsx └── hooks │ ├── useDisplay.tsx │ ├── useImage.tsx │ └── useText.tsx ├── jest.config.js ├── .github └── workflows │ ├── size.yml │ └── main.yml ├── tsdx.config.js ├── test └── index.test.tsx ├── LICENSE ├── tsconfig.json ├── README.md └── package.json /example/.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .cache 3 | dist -------------------------------------------------------------------------------- /scrutinizer.yml: -------------------------------------------------------------------------------- 1 | build: 2 | environment: 3 | node: v14.0 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .DS_Store 3 | node_modules 4 | .cache 5 | dist 6 | .idea 7 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lluiscamino/fake-tweet/HEAD/example.png -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import Tweet from './components/tweet/Tweet'; 2 | 3 | export default Tweet; 4 | -------------------------------------------------------------------------------- /example/images/npm-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lluiscamino/fake-tweet/HEAD/example/images/npm-icon.png -------------------------------------------------------------------------------- /example/images/github-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lluiscamino/fake-tweet/HEAD/example/images/github-icon.png -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | //jest.config.js 2 | 3 | module.exports = { 4 | moduleNameMapper: { 5 | "\\.(css|sass)$": "identity-obj-proxy", 6 | }, 7 | }; 8 | -------------------------------------------------------------------------------- /src/types/UserData.ts: -------------------------------------------------------------------------------- 1 | type UserData = { 2 | nickname: string; 3 | name: string; 4 | avatar: string; 5 | verified: boolean; 6 | locked: boolean; 7 | }; 8 | 9 | export default UserData; 10 | -------------------------------------------------------------------------------- /.github/workflows/size.yml: -------------------------------------------------------------------------------- 1 | name: size 2 | on: [pull_request] 3 | jobs: 4 | size: 5 | runs-on: ubuntu-latest 6 | env: 7 | CI_JOB_NUMBER: 1 8 | steps: 9 | - uses: actions/checkout@v1 10 | - uses: andresz1/size-limit-action@v1 11 | with: 12 | github_token: ${{ secrets.GITHUB_TOKEN }} 13 | -------------------------------------------------------------------------------- /src/types/TweetConfig.ts: -------------------------------------------------------------------------------- 1 | import UserData from './UserData'; 2 | 3 | type TweetConfig = { 4 | user: UserData; 5 | display: string; 6 | text: string; 7 | image?: string | string[]; 8 | date: string; 9 | app: string; 10 | retweets: number; 11 | quotedTweets: number; 12 | likes: number; 13 | }; 14 | 15 | export default TweetConfig; 16 | -------------------------------------------------------------------------------- /src/components/tweet/Metadata.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import TweetConfig from '../../types/TweetConfig'; 3 | 4 | function Metadata({ config }: { config: TweetConfig }) { 5 | return ( 6 |
7 | {config.date} · {config.app} 8 |
9 | ); 10 | } 11 | 12 | export default Metadata; 13 | -------------------------------------------------------------------------------- /tsdx.config.js: -------------------------------------------------------------------------------- 1 | const postcss = require('rollup-plugin-postcss'); 2 | 3 | module.exports = { 4 | rollup(config, options) { 5 | config.plugins.push( 6 | postcss({ 7 | modules: false, 8 | inject: true, 9 | extract: false, 10 | }), 11 | ); 12 | return config; 13 | }, 14 | }; 15 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Playground 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/hooks/useDisplay.tsx: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from 'react'; 2 | 3 | function useDisplay(rawDisplay: any) { 4 | const [display, setDisplay] = useState([]); 5 | useEffect(() => { 6 | const validDisplay = ['default', 'dim', 'lightsout'].includes(rawDisplay); 7 | setDisplay(validDisplay ? rawDisplay : 'default'); 8 | }, [rawDisplay]); 9 | return display; 10 | } 11 | 12 | export default useDisplay; 13 | -------------------------------------------------------------------------------- /src/components/icons/DropIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | function DropIcon() { 4 | return ( 5 | 6 | 7 | 8 | 9 | 10 | ); 11 | } 12 | 13 | export default DropIcon; 14 | -------------------------------------------------------------------------------- /src/hooks/useImage.tsx: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from 'react'; 2 | 3 | function useImage(rawImage?: string | string[]) { 4 | const [image, setImage] = useState([]); 5 | useEffect(() => { 6 | let imgArray: string[]; 7 | if (!rawImage) { 8 | imgArray = []; 9 | } else { 10 | imgArray = rawImage && Array.isArray(rawImage) ? rawImage : [rawImage]; 11 | } 12 | setImage(imgArray); 13 | }, [rawImage]); 14 | return image; 15 | } 16 | 17 | export default useImage; 18 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": false, 4 | "target": "es5", 5 | "module": "commonjs", 6 | "jsx": "react", 7 | "moduleResolution": "node", 8 | "noImplicitAny": false, 9 | "noUnusedLocals": false, 10 | "noUnusedParameters": false, 11 | "removeComments": true, 12 | "strictNullChecks": true, 13 | "preserveConstEnums": true, 14 | "sourceMap": true, 15 | "lib": ["es2015", "es2016", "dom"], 16 | "types": ["node"] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/components/tweet/Content.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import TweetConfig from '../../types/TweetConfig'; 3 | import useText from '../../hooks/useText'; 4 | import ImagesContainer from './ImagesContainer'; 5 | 6 | function Content({ config }: { config: TweetConfig }) { 7 | const text = useText(config.text); 8 | 9 | return ( 10 |
11 | {text &&
{text}
} 12 | 13 |
14 | ); 15 | } 16 | 17 | export default Content; 18 | -------------------------------------------------------------------------------- /src/components/icons/LikeIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | function LikeIcon() { 4 | return ( 5 | 6 | 7 | 8 | 9 | 10 | ); 11 | } 12 | 13 | export default LikeIcon; 14 | -------------------------------------------------------------------------------- /src/components/icons/ShareIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | function ShareIcon() { 4 | return ( 5 | 6 | 7 | 8 | 9 | 10 | 11 | ); 12 | } 13 | 14 | export default ShareIcon; 15 | -------------------------------------------------------------------------------- /src/components/icons/CommentIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | function CommentIcon() { 4 | return ( 5 | 6 | 7 | 8 | 9 | 10 | ); 11 | } 12 | 13 | export default CommentIcon; 14 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "scripts": { 7 | "start": "parcel index.html", 8 | "build": "parcel build index.html" 9 | }, 10 | "dependencies": { 11 | "images": "^3.2.4", 12 | "react-app-polyfill": "^1.0.0" 13 | }, 14 | "alias": { 15 | "react": "../node_modules/react", 16 | "react-dom": "../node_modules/react-dom/profiling", 17 | "scheduler/tracing": "../node_modules/scheduler/tracing-profiling" 18 | }, 19 | "devDependencies": { 20 | "@types/react": "^16.9.11", 21 | "@types/react-dom": "^16.8.4", 22 | "parcel": "^1.12.3", 23 | "typescript": "^3.4.5" 24 | }, 25 | "resolutions": { 26 | "@babel/preset-env": "7.13.8" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/components/tweet/Tweet.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './Tweet.css'; 3 | import useDisplay from '../../hooks/useDisplay'; 4 | import TweetConfig from '../../types/TweetConfig'; 5 | import UserInfo from './UserInfo'; 6 | import Content from './Content'; 7 | import Metadata from './Metadata'; 8 | import Impact from './Impact'; 9 | import Actions from './Actions'; 10 | 11 | function Tweet({ config }: { config: TweetConfig }) { 12 | const display = useDisplay(config.display); 13 | 14 | return ( 15 |
16 | 17 | 18 | 19 | 20 | 21 |
22 | ); 23 | } 24 | 25 | export default Tweet; 26 | -------------------------------------------------------------------------------- /src/components/icons/LockIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | function LockIcon() { 4 | return ( 5 | 10 | 11 | 12 | 13 | 14 | ); 15 | } 16 | 17 | export default LockIcon; 18 | -------------------------------------------------------------------------------- /test/index.test.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import * as ReactDOM from 'react-dom'; 3 | import Tweet from '../src'; 4 | 5 | describe('it', () => { 6 | const config = { 7 | user: { 8 | nickname: 'twitter', 9 | name: 'Twitter', 10 | avatar: 'avatar.png', 11 | verified: true, 12 | locked: false, 13 | }, 14 | display: 'default', 15 | text: 'This is a fake tweet', 16 | image: '', 17 | date: '3:32 PM · Feb 14, 1997', 18 | app: 'Twitter for iPhone', 19 | retweets: 32000, 20 | quotedTweets: 100, 21 | likes: 12700, 22 | }; 23 | 24 | it('renders without crashing', () => { 25 | const div = document.createElement('div'); 26 | ReactDOM.render(, div); 27 | ReactDOM.unmountComponentAtNode(div); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /src/components/icons/RetweetIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | function RetweetIcon() { 4 | return ( 5 | 6 | 7 | 8 | 9 | 10 | ); 11 | } 12 | 13 | export default RetweetIcon; 14 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push] 3 | jobs: 4 | build: 5 | name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }} 6 | 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | matrix: 10 | node: ['14.x', '16.x'] 11 | os: [ubuntu-latest, windows-latest, macOS-latest] 12 | 13 | steps: 14 | - name: Checkout repo 15 | uses: actions/checkout@v2 16 | 17 | - name: Use Node ${{ matrix.node }} 18 | uses: actions/setup-node@v1 19 | with: 20 | node-version: ${{ matrix.node }} 21 | 22 | - name: Install deps and build (with cache) 23 | uses: bahmutov/npm-install@v1 24 | 25 | - name: Lint 26 | run: yarn lint 27 | 28 | - name: Test 29 | run: yarn test --ci --coverage --maxWorkers=2 30 | 31 | - name: Build 32 | run: yarn build 33 | -------------------------------------------------------------------------------- /src/components/tweet/Actions.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import CommentIcon from '../icons/CommentIcon'; 3 | import RetweetIcon from '../icons/RetweetIcon'; 4 | import LikeIcon from '../icons/LikeIcon'; 5 | import ShareIcon from '../icons/ShareIcon'; 6 | 7 | function Actions() { 8 | const actions = [ 9 | { 10 | color: 'blue', 11 | icon: , 12 | }, 13 | { 14 | color: 'green', 15 | icon: , 16 | }, 17 | { 18 | color: 'red', 19 | icon: , 20 | }, 21 | { 22 | color: 'blue', 23 | icon: , 24 | }, 25 | ]; 26 | return ( 27 |
28 | {actions.map((action, key) => ( 29 |
30 |
{action.icon}
31 |
32 | ))} 33 |
34 | ); 35 | } 36 | 37 | export default Actions; 38 | -------------------------------------------------------------------------------- /src/components/icons/VerifiedIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | function VerifiedIcon() { 4 | return ( 5 | 10 | 11 | 12 | 13 | 14 | ); 15 | } 16 | 17 | export default VerifiedIcon; 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Lluís Camino 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. -------------------------------------------------------------------------------- /src/components/tweet/UserInfo.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import TweetConfig from '../../types/TweetConfig'; 3 | import DropIcon from '../icons/DropIcon'; 4 | import VerifiedIcon from '../icons/VerifiedIcon'; 5 | import LockIcon from '../icons/LockIcon'; 6 | import Twemoji from 'react-twemoji'; 7 | 8 | function UserInfo({ config }: { config: TweetConfig }) { 9 | return ( 10 |
11 |
12 | {config.user.name 17 |
18 |
19 |
20 | 21 |
22 |
23 | 28 | {config.user.name} 29 | 30 | {config.user.verified && ( 31 |
32 | 33 |
34 | )} 35 | {config.user.locked && !config.user.verified && ( 36 |
37 | 38 |
39 | )} 40 |
41 |
@{config.user.nickname}
42 |
43 |
44 | ); 45 | } 46 | 47 | export default UserInfo; 48 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | // see https://www.typescriptlang.org/tsconfig to better understand tsconfigs 3 | "include": ["src", "types"], 4 | "compilerOptions": { 5 | "module": "esnext", 6 | "lib": ["dom", "esnext"], 7 | "importHelpers": true, 8 | // output .d.ts declaration files for consumers 9 | "declaration": true, 10 | // output .js.map sourcemap files for consumers 11 | "sourceMap": true, 12 | // match output dir to input dir. e.g. dist/index instead of dist/src/index 13 | "rootDir": "./src", 14 | // stricter type-checking for stronger correctness. Recommended by TS 15 | "strict": true, 16 | // linter checks for common issues 17 | "noImplicitReturns": true, 18 | "noFallthroughCasesInSwitch": true, 19 | // noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative 20 | "noUnusedLocals": true, 21 | "noUnusedParameters": true, 22 | // use Node's module resolution algorithm, instead of the legacy TS one 23 | "moduleResolution": "node", 24 | // transpile JSX to React.createElement 25 | "jsx": "react", 26 | // interop between ESM and CJS modules. Recommended by TS 27 | "esModuleInterop": true, 28 | // significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS 29 | "skipLibCheck": true, 30 | // error out if import and file system have a casing mismatch. Recommended by TS 31 | "forceConsistentCasingInFileNames": true, 32 | // `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc` 33 | "noEmit": true 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/components/tweet/Impact.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import TweetConfig from '../../types/TweetConfig'; 3 | 4 | function Impact({ config }: { config: TweetConfig }) { 5 | if ( 6 | config.retweets === 0 && 7 | config.quotedTweets === 0 && 8 | config.likes === 0 9 | ) { 10 | return <>; 11 | } 12 | return ( 13 |
14 | {config.retweets !== 0 && ( 15 | 16 | {styleNumber(config.retweets)}{' '} 17 | {getText('Retweet', config.retweets)} 18 | 19 | )} 20 | {config.quotedTweets !== 0 && ( 21 | 22 | {styleNumber(config.quotedTweets)}{' '} 23 | {getText('Quoted Tweet', config.quotedTweets)} 24 | 25 | )} 26 | {config.likes !== 0 && ( 27 | 28 | {styleNumber(config.likes)}{' '} 29 | {getText('Like', config.likes)} 30 | 31 | )} 32 |
33 | ); 34 | 35 | function styleNumber(num: number) { 36 | let div = num / 1000000; 37 | if (div >= 1) { 38 | return ( 39 | div.toFixed(1).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, '$1') + 'M' 40 | ); 41 | } 42 | div = num / 1000; 43 | if (div >= 1) { 44 | return ( 45 | div.toFixed(1).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, '$1') + 'K' 46 | ); 47 | } 48 | return num; 49 | } 50 | 51 | function getText(text: string, count: number) { 52 | return count === 1 ? text : text + 's'; 53 | } 54 | } 55 | 56 | export default Impact; 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fake Tweet 2 | 3 | Tweet React component. See a [live demo](https://lluiscamino.github.io/fake-tweet/). 4 | 5 | ## Example 6 | 7 | ![Fake tweet example](https://github.com/lluiscamino/fake-tweet/blob/master/example.png?raw=true) 8 | 9 | ## Installation 10 | 11 | ```bash 12 | npm i fake-tweet 13 | ``` 14 | 15 | ## Usage 16 | 17 | The following snippet is an example of use of the `fake-tweet` component: 18 | 19 | ```javascript 20 | import React from "react"; 21 | import FakeTweet from "fake-tweet"; 22 | 23 | function App() { 24 | const config = { 25 | user: { 26 | nickname: "twitter", 27 | name: "Twitter", 28 | avatar: "avatar.png", 29 | verified: true, 30 | locked: false 31 | }, 32 | display: "default", 33 | text: "This is a fake tweet", 34 | image: "", 35 | date: "3:32 PM · Feb 14, 1997", 36 | app: "Twitter for iPhone", 37 | retweets: 32000, 38 | quotedTweets: 100, 39 | likes: 12700 40 | }; 41 | return ( 42 |
43 | 44 |
45 | ); 46 | } 47 | 48 | export default App; 49 | ``` 50 | 51 | You also need to pass a `config` object to the component with the following properties: 52 | 53 | - **User**: 54 | - **Nickname**: Twitter @username 55 | - **Name** 56 | - **Avatar**: Twitter avatar URL 57 | - **Verified**: Set to true to include the verified icon 58 | - **Locked**: Set to true to include the private account icon 59 | - **Display**: Twitter theme (``default``, ``dim`` or ``lightsout``) 60 | - **Text**: The text the tweet will display 61 | - **Image (optional)**: You can include an image to the tweet. Can be either a single URL string or an array of up to four URL strings for multiple images. 62 | - **Date**: A string that represents a date 63 | - **App**: For example, "Twitter for iPhone" 64 | - **Retweets**: Number of retweets 65 | - **Quoted Tweets**: Number of quoted tweets 66 | - **Likes**: Number of likes 67 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.2", 3 | "license": "MIT", 4 | "main": "dist/index.js", 5 | "typings": "dist/index.d.ts", 6 | "files": [ 7 | "dist", 8 | "src" 9 | ], 10 | "engines": { 11 | "node": ">=10" 12 | }, 13 | "scripts": { 14 | "start": "tsdx watch", 15 | "build": "tsdx build", 16 | "test": "tsdx test --passWithNoTests", 17 | "lint": "tsdx lint", 18 | "prepare": "tsdx build", 19 | "size": "size-limit", 20 | "analyze": "size-limit --why" 21 | }, 22 | "peerDependencies": { 23 | "react": ">=16" 24 | }, 25 | "husky": { 26 | "hooks": { 27 | "pre-commit": "tsdx lint" 28 | } 29 | }, 30 | "prettier": { 31 | "printWidth": 80, 32 | "semi": true, 33 | "singleQuote": true, 34 | "trailingComma": "es5" 35 | }, 36 | "name": "fake-tweet", 37 | "description": "Tweet React Component", 38 | "keywords": [ 39 | "tweet", 40 | "fake-tweet", 41 | "twitter", 42 | "fake", 43 | "react", 44 | "component" 45 | ], 46 | "author": "Lluís Camino", 47 | "homepage": "https://lluiscamino.github.io/fake-tweet", 48 | "module": "dist/fake-tweet.esm.js", 49 | "size-limit": [ 50 | { 51 | "path": "dist/fake-tweet.cjs.production.min.js", 52 | "limit": "15 KB" 53 | }, 54 | { 55 | "path": "dist/fake-tweet.esm.js", 56 | "limit": "15 KB" 57 | } 58 | ], 59 | "devDependencies": { 60 | "@size-limit/preset-small-lib": "^7.0.8", 61 | "@types/react": "^18.0.9", 62 | "@types/react-dom": "^18.0.5", 63 | "@types/react-twemoji": "^0.4.0", 64 | "husky": "^8.0.1", 65 | "identity-obj-proxy": "^3.0.0", 66 | "postcss": "^8.4.14", 67 | "react": "^18.1.0", 68 | "react-dom": "^18.1.0", 69 | "rollup-plugin-postcss": "^4.0.2", 70 | "size-limit": "^7.0.8", 71 | "tsdx": "^0.14.1", 72 | "tslib": "^2.4.0", 73 | "typescript": "^4.7.2" 74 | }, 75 | "dependencies": { 76 | "react-process-string": "^1.2.0", 77 | "react-twemoji": "^0.5.0" 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/hooks/useText.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from 'react'; 2 | // @ts-ignore 3 | import processString from 'react-process-string'; 4 | import Twemoji from 'react-twemoji'; 5 | 6 | function useText(rawText: any) { 7 | const [text, setText] = useState(rawText); 8 | useEffect(() => { 9 | setText( 10 | processString([ 11 | { 12 | regex: /(?:^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@)(?!\/))([a-zA-Z0-9/_]{1,15})(?:\b(?!@|@)|$)/, 13 | fn: handleMention, 14 | }, 15 | { 16 | regex: /(?:^|[^a-zA-Z0-9_@!@#$%&*])(?:#(?!\/))([a-zA-Z0-9/_]{1,280})(?:\b(?!#)|$)/, 17 | fn: handleHashtag, 18 | }, 19 | { 20 | regex: /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/, 21 | fn: handleEmoji, 22 | }, 23 | { 24 | regex: /(?:^|[^a-zA-Z0-9_@!@#$%&*])(?:https?:\/\/)(\S+\.\S+)/, 25 | fn: handleUrl, 26 | }, 27 | ])(rawText) 28 | ); 29 | }, [rawText]); 30 | 31 | return text; 32 | } 33 | 34 | function handleMention(key: string, result: string[]) { 35 | return ( 36 | 37 | {' '} 38 | @{result[1]} 39 | 40 | ); 41 | } 42 | 43 | function handleHashtag(key: string, result: string[]) { 44 | return ( 45 | 46 | {' '} 47 | #{result[1]} 48 | 49 | ); 50 | } 51 | 52 | function handleUrl(key: string, result: string[]) { 53 | return ( 54 | 55 | {' '} 56 | {result[1]} 57 | 58 | ); 59 | } 60 | 61 | function handleEmoji(key: string, result: string[]) { 62 | return ( 63 | 69 | {result[1]} 70 | 71 | ); 72 | } 73 | 74 | export default useText; 75 | -------------------------------------------------------------------------------- /example/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | .App { 11 | text-align: center; 12 | background-color: #fdfdfd; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | color: #000000; 19 | position: relative; 20 | } 21 | 22 | #content { 23 | padding-bottom: 50px; 24 | } 25 | 26 | .App-link { 27 | color: #61dafb; 28 | } 29 | 30 | .config { 31 | margin-top: 20px; 32 | } 33 | 34 | .config label { 35 | font-weight: bold; 36 | display: inline-block; 37 | width: 100px; 38 | text-align: right; 39 | margin-right: 20px; 40 | } 41 | 42 | .config input, textarea, select { 43 | width: 200px; 44 | padding: 4px; 45 | border: 1px solid #e6ecf0; 46 | color: #14171a; 47 | font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif; 48 | background: #fff; 49 | font-size: 13px; 50 | } 51 | 52 | .config textarea { 53 | height: 160px; 54 | min-height: 100px; 55 | max-height: 180px; 56 | min-width: 200px; 57 | max-width: 200px; 58 | } 59 | 60 | fieldset { 61 | font-size: 15px; 62 | text-align: left; 63 | padding: 15px; 64 | border: 1px solid #e6ecf0; 65 | color: #14171a; 66 | font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif; 67 | background: #fff; 68 | } 69 | 70 | fieldset legend { 71 | font-weight: bold; 72 | } 73 | 74 | #gh-ribbon { 75 | position: fixed; 76 | top: 0; 77 | right: 0; 78 | } 79 | 80 | @media only screen and (min-device-width : 320px) and (max-device-width : 480px) { 81 | #gh-ribbon { 82 | display: none; 83 | } 84 | } 85 | 86 | footer { 87 | text-align: left; 88 | font-size: 12px; 89 | position: absolute; 90 | bottom: 0; 91 | width: 100%; 92 | height: 25px; 93 | border-top: 1px solid #e6ecf0; 94 | padding-top: 10px; 95 | } 96 | 97 | #copyright { 98 | margin-left: 20px; 99 | } 100 | 101 | #footer-links { 102 | float: right; 103 | margin-right: 20px; 104 | } 105 | 106 | .footer-icon { 107 | margin-left: 5px; 108 | height: 16px; 109 | width: 16px; 110 | } 111 | -------------------------------------------------------------------------------- /src/components/tweet/ImagesContainer.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import useImage from '../../hooks/useImage'; 3 | import TweetConfig from '../../types/TweetConfig'; 4 | 5 | function ImagesContainer({ config }: { config: TweetConfig }) { 6 | const image = useImage(config.image); 7 | 8 | switch (image.length) { 9 | case 1: 10 | return ( 11 |
12 | 13 |
14 | ); 15 | case 2: 16 | return ( 17 |
18 | 19 |
20 | 21 |
22 | ); 23 | case 3: 24 | return ( 25 |
26 | 27 |
28 | 29 |
30 | ); 31 | case 4: 32 | return ( 33 |
34 | 35 |
36 | 37 |
38 | ); 39 | default: 40 | return <>; 41 | } 42 | 43 | function OneImageColumn({ side, image }: { side: string; image: string }) { 44 | side = sanitizeSide(side); 45 | const borderSide = getBorderSide(side); 46 | return ( 47 |
50 | 51 |
52 | ); 53 | } 54 | 55 | function TwoImagesColumn({ 56 | side, 57 | image1, 58 | image2, 59 | }: { 60 | side: string; 61 | image1: string; 62 | image2: string; 63 | }) { 64 | side = sanitizeSide(side); 65 | const borderSide = getBorderSide(side); 66 | return ( 67 |
68 |
69 | 70 |
71 |
72 |
73 | 74 |
75 |
76 | ); 77 | } 78 | 79 | function sanitizeSide(side: string) { 80 | return side === 'right' || side === 'left' ? side : 'left'; 81 | } 82 | 83 | function getBorderSide(side: string) { 84 | return side.charAt(0); 85 | } 86 | } 87 | 88 | export default ImagesContainer; 89 | -------------------------------------------------------------------------------- /src/components/tweet/Tweet.css: -------------------------------------------------------------------------------- 1 | @media all and (min-width: 0px) and (max-width: 425px) { 2 | .tweet { 3 | width: 90% !important; 4 | } 5 | } 6 | 7 | .tweet { 8 | font-size: 15px; 9 | text-align: left; 10 | padding: 15px 15px 0 15px; 11 | max-width: 600px; 12 | width: 60%; 13 | border: 1px solid #e6ecf0; 14 | color: rgb(20, 23, 26); 15 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; 16 | background: #ffffff; 17 | overflow: hidden; 18 | } 19 | 20 | .user-info-right { 21 | padding-top: 2px; 22 | } 23 | 24 | .user-name { 25 | font-weight: bold; 26 | } 27 | 28 | .user-name-txt { 29 | display: inline; 30 | } 31 | 32 | .user-nickname { 33 | cursor: pointer; 34 | color: rgb(101, 119, 134); 35 | } 36 | 37 | .icon { 38 | margin-left: 3px; 39 | display: inline; 40 | position: relative; 41 | top: 4px; 42 | } 43 | 44 | .icon svg { 45 | height: 1.25em; 46 | } 47 | 48 | .verified-icon-svg { 49 | fill: rgba(29, 161, 242, 1.00); 50 | } 51 | 52 | .tweet-content { 53 | padding-top: 6px; 54 | font-size: 23px; 55 | clear: left; 56 | } 57 | 58 | .tweet-content .txt { 59 | padding-bottom: 15px; 60 | } 61 | 62 | .metadata { 63 | color: rgb(101, 119, 134); 64 | padding-bottom: 15px; 65 | border-bottom: 1px solid #e6ecf0; 66 | } 67 | 68 | .rt-likes { 69 | padding-top: 15px; 70 | padding-bottom: 15px; 71 | border-bottom: 1px solid #e6ecf0; 72 | color: rgb(101, 119, 134); 73 | } 74 | 75 | .rt-likes strong { 76 | color: rgb(20, 23, 26); 77 | } 78 | 79 | .rt-likes .fake-link:not(:first-child) { 80 | margin-left: 20px; 81 | } 82 | 83 | .mention, .hashtag { 84 | color: rgb(27, 149, 224); 85 | } 86 | 87 | .fake-link { 88 | cursor: pointer; 89 | } 90 | 91 | .fake-link:hover { 92 | text-decoration: underline; 93 | } 94 | 95 | .avatar-container { 96 | margin-right: 10px; 97 | float: left; 98 | } 99 | 100 | .avatar { 101 | height: 49px; 102 | width: 49px; 103 | border-radius: 50%; 104 | cursor: pointer; 105 | } 106 | 107 | .drop-button { 108 | height: 27px; 109 | width: 27px; 110 | display: flex; 111 | align-items: center; 112 | justify-content: center; 113 | cursor: pointer; 114 | border-radius: 50%; 115 | float: right; 116 | transition: 0.2s; 117 | } 118 | 119 | .drop-button svg { 120 | width: 1em; 121 | height: 1em; 122 | fill: rgb(101, 119, 134); 123 | } 124 | 125 | .drop-button:hover { 126 | background-color: rgba(29, 161, 242, 0.1); 127 | } 128 | 129 | .drop-button svg:hover { 130 | fill: rgb(27, 149, 224); 131 | } 132 | 133 | .bottom-buttons { 134 | display: flex; 135 | flex-direction: row; 136 | } 137 | 138 | .bottom-button { 139 | height: 48px; 140 | width: 48px; 141 | display: inline-block; 142 | cursor: pointer; 143 | flex: 0 25%; 144 | box-sizing: border-box; 145 | } 146 | 147 | .bottom-button > div { 148 | display: flex; 149 | align-items: center; 150 | height: 100%; 151 | } 152 | 153 | .bottom-button svg { 154 | padding: 7px; 155 | border-radius: 50%; 156 | margin: auto; 157 | fill: rgb(101, 119, 134); 158 | height: 1.5em; 159 | width: 1.6em; 160 | transition: 0.2s; 161 | } 162 | 163 | .bottom-button.blue svg:hover { 164 | fill: rgb(27, 149, 224); 165 | } 166 | 167 | .bottom-button.green svg:hover { 168 | fill: rgb(23, 191, 99); 169 | } 170 | 171 | .bottom-button.red svg:hover { 172 | fill: rgb(215, 42, 94); 173 | } 174 | 175 | .bottom-button.blue svg:hover { 176 | background-color: rgba(29, 161, 242, 0.1); 177 | } 178 | 179 | .bottom-button.green svg:hover { 180 | background-color: rgba(23, 191, 99, 0.1); 181 | } 182 | 183 | .bottom-button.red svg:hover { 184 | background-color: rgba(215, 42, 94, 0.1); 185 | } 186 | 187 | /* Images */ 188 | .image-container { 189 | margin-top: 10px; 190 | margin-bottom: 10px; 191 | cursor: pointer; 192 | } 193 | 194 | .image-container img { 195 | border-radius: 13px; 196 | border: 1px solid #ccd6dd; 197 | width: 100%; 198 | } 199 | 200 | .images-container { 201 | margin-top: 10px; 202 | margin-bottom: 10px; 203 | width: 100%; 204 | height: 318px; 205 | border-radius: 13px; 206 | border: 1px solid #ccd6dd; 207 | } 208 | 209 | .images-container img { 210 | cursor: pointer; 211 | } 212 | 213 | .full-height-image { 214 | width: calc(50% - 1px); 215 | height: 100%; 216 | overflow: hidden; 217 | } 218 | 219 | .full-height-image img { 220 | height: 100%; 221 | min-width: 100%; 222 | } 223 | 224 | .left-image { 225 | float: left; 226 | } 227 | 228 | .half-height-image { 229 | height: 158px; 230 | overflow: hidden; 231 | } 232 | 233 | .half-height-image img { 234 | width: 100%; 235 | min-height: 158px; 236 | } 237 | 238 | .images-container .right-col, .left-col { 239 | float: left; 240 | width: calc(50% - 1px); 241 | height: 100%; 242 | } 243 | 244 | .images-container .horizontal-spacer { 245 | height: 2px; 246 | overflow: hidden; 247 | } 248 | 249 | .vertical-spacer { 250 | width: 2px; 251 | height: 100%; 252 | float: left; 253 | } 254 | 255 | .tl { 256 | border-top-left-radius: 13px; 257 | } 258 | 259 | .bl { 260 | border-bottom-left-radius: 13px; 261 | } 262 | 263 | .tr { 264 | border-top-right-radius: 13px; 265 | } 266 | 267 | .br { 268 | border-bottom-right-radius: 13px; 269 | } 270 | 271 | .twemoji-sm, .twemoji-bg { 272 | position: relative; 273 | top: 4px; 274 | } 275 | 276 | .twemoji-sm { 277 | height: 18px; 278 | width: 18px; 279 | } 280 | 281 | .twemoji-bg { 282 | height: 28px; 283 | width: 28px; 284 | } 285 | 286 | .tweet svg { 287 | box-sizing: content-box !important; 288 | vertical-align: baseline !important; 289 | } 290 | 291 | /* Lights out */ 292 | .tweet.lightsout { 293 | background: #000000; 294 | border-color: rgb(47, 51, 54); 295 | color: rgb(217, 217, 217); 296 | } 297 | 298 | .lightsout .rt-likes strong { 299 | color: rgb(217, 217, 217); 300 | } 301 | 302 | .lightsout .rt-likes, .lightsout .metadata, .lightsout .image-container img, .lightsout .images-container { 303 | border-color: rgb(47, 51, 54); 304 | } 305 | 306 | .lightsout .verified-icon-svg, .lightsout .lock-icon-svg { 307 | fill: rgba(217, 217, 217, 1.00); 308 | } 309 | 310 | /* Dim */ 311 | .tweet.dim { 312 | background: rgb(21, 32, 43); 313 | border-color: #38444c; 314 | color: rgb(255, 255, 255); 315 | } 316 | 317 | .dim .rt-likes strong { 318 | color: rgb(255, 255, 255); 319 | } 320 | 321 | .dim .rt-likes, .dim .metadata, .dim .image-container img, .dim .images-container { 322 | border-color: rgb(47, 51, 54); 323 | } 324 | 325 | .dim .verified-icon-svg, .dim .lock-icon-svg { 326 | fill: rgb(255, 255, 255); 327 | } 328 | -------------------------------------------------------------------------------- /example/index.tsx: -------------------------------------------------------------------------------- 1 | import 'react-app-polyfill/ie11'; 2 | import * as React from 'react'; 3 | import {useState} from 'react'; 4 | import * as ReactDOM from 'react-dom'; 5 | import Tweet from '../src'; 6 | import './index.css'; 7 | 8 | const App = () => { 9 | const [nickname, setNickname] = useState('SpaceX'); 10 | const [name, setName] = useState('SpaceX'); 11 | const [avatar, setAvatar] = useState('https://pbs.twimg.com/profile_images/1082744382585856001/rH_k3PtQ_normal.jpg'); 12 | const [verified, setVerified] = useState(true); 13 | const [locked, setLocked] = useState(false); 14 | const [display, setDisplay] = useState('default'); 15 | const [text, setText] = useState('Falcon 9 launches Starlink to orbit – the eighth launch and landing of this booster 🚀'); 16 | const [image, setImage] = useState([ 17 | 'https://pbs.twimg.com/media/EsL2cuZUwAEIM-N?format=jpg&name=small', 18 | 'https://pbs.twimg.com/media/EsL2cuyVQAA8j4D?format=jpg&name=small', 19 | 'https://pbs.twimg.com/media/EsL2cvxUYAAvdih?format=jpg&name=small', 20 | 'https://pbs.twimg.com/media/EsL2cwGVcAIKGq3?format=jpg&name=small' 21 | ]); 22 | const [date, setDate] = useState('4:26 PM · Jan 20, 2021'); 23 | const [app, setApp] = useState('Twitter Web App'); 24 | const [retweets, setRetweets] = useState(5703); 25 | const [quotedTweets, setQuotedTweets] = useState(379); 26 | const [likes, setLikes] = useState(68900); 27 | 28 | return ( 29 |
30 | 32 | Fork me on GitHub 38 | 39 | 58 |
59 |
60 |
61 | User 62 |
63 | 64 | setNickname(e.target.value)}/> 66 |
67 |
68 | 69 | setName(e.target.value)}/> 70 |
71 |
72 | 73 | setAvatar(e.target.value)}/> 75 |
76 |
77 | 78 | { 79 | const val = e.target.checked; 80 | setVerified(val); 81 | if (val && locked) setLocked(false); 82 | }}/> 83 |
84 |
85 | 86 | { 87 | const val = e.target.checked; 88 | setLocked(val); 89 | if (val && verified) setVerified(false); 90 | }}/> 91 |
92 |
93 |
94 | Tweet 95 |
96 | 97 | 104 |
105 |
106 | 107 |