react-obfuscate-email
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,9 +0,10 @@ | ||
import { FC, HTMLAttributes, ReactChild } from 'react'; | ||
export interface Props extends HTMLAttributes<HTMLDivElement> { | ||
/** custom content, defaults to 'the snozzberries taste like snozzberries' */ | ||
children?: ReactChild; | ||
} | ||
/** | ||
* A custom Thing component. Neat! | ||
*/ | ||
export declare const Thing: FC<Props>; | ||
import { AnchorHTMLAttributes, DetailedHTMLProps, ReactNode } from 'react'; | ||
declare type Props = DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement> & { | ||
body?: string; | ||
children?: ReactNode; | ||
className?: string; | ||
email: string; | ||
subject?: string; | ||
}; | ||
export declare function Email({ body, subject, children, className, email, ...rest }: Props): JSX.Element; | ||
export {}; |
@@ -7,16 +7,104 @@ 'use strict'; | ||
var React = _interopDefault(require('react')); | ||
var React = require('react'); | ||
var React__default = _interopDefault(React); | ||
var styled = _interopDefault(require('styled-components')); | ||
// see: https://github.com/storybookjs/storybook/issues/9556 | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
/** | ||
* A custom Thing component. Neat! | ||
*/ | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
var Thing = function Thing(_ref) { | ||
var children = _ref.children; | ||
return React.createElement("div", null, children || "the snozzberries taste like snozzberries"); | ||
}; | ||
return target; | ||
} | ||
exports.Thing = Thing; | ||
function _taggedTemplateLiteralLoose(strings, raw) { | ||
if (!raw) { | ||
raw = strings.slice(0); | ||
} | ||
strings.raw = raw; | ||
return strings; | ||
} | ||
function isValidParam(param) { | ||
var value = param[1]; | ||
return typeof value === 'string' && value.length > 0; | ||
} | ||
/* | ||
Do not use URLSearchParams. | ||
URLSearchParams turns spaces into '+': | ||
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#examples | ||
Some email clients on mobile apps will render the '+' and not properly replace it with spaces. | ||
For the 'mailto' protocol, spaces should be percent encoded according to the spec: | ||
https://datatracker.ietf.org/doc/html/rfc6068#ref-STD66 | ||
*/ | ||
function percentEncodeParams(params) { | ||
return Object.entries(params).filter(isValidParam).map(function (_ref) { | ||
var key = _ref[0], | ||
value = _ref[1]; | ||
return key + "=" + encodeURIComponent(value); | ||
}).join('&'); | ||
} | ||
var _excluded = ["body", "subject", "children", "className", "email"]; | ||
var _templateObject; | ||
var Obfuscated = /*#__PURE__*/styled.span(_templateObject || (_templateObject = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n &::after {\n content: '@';\n }\n"]))); | ||
function obfuscateEmail(email) { | ||
var _email$split = email.split('@'), | ||
username = _email$split[0], | ||
domain = _email$split[1]; | ||
return React__default.createElement(React__default.Fragment, null, username, React__default.createElement(Obfuscated, null), domain); | ||
} | ||
function Email(_ref) { | ||
var _ref$body = _ref.body, | ||
body = _ref$body === void 0 ? '' : _ref$body, | ||
_ref$subject = _ref.subject, | ||
subject = _ref$subject === void 0 ? '' : _ref$subject, | ||
children = _ref.children, | ||
className = _ref.className, | ||
email = _ref.email, | ||
rest = _objectWithoutPropertiesLoose(_ref, _excluded); | ||
var _useState = React.useState(false), | ||
hovered = _useState[0], | ||
setHovered = _useState[1]; | ||
var emailUrl = new URL("mailto:" + email); // https://github.com/whatwg/url/issues/18#issuecomment-369865339 | ||
emailUrl.search = percentEncodeParams({ | ||
body: body, | ||
subject: subject | ||
}); | ||
function handleHover() { | ||
setHovered(true); | ||
} | ||
var displayText = children || email; | ||
var obfuscatedText = children || obfuscateEmail(email); | ||
return React__default.createElement("a", Object.assign({ | ||
className: className, | ||
href: hovered ? emailUrl.href : '#', | ||
onFocus: handleHover, | ||
onMouseOver: handleHover | ||
}, rest), hovered ? displayText : obfuscatedText); | ||
} | ||
exports.Email = Email; | ||
//# sourceMappingURL=react-obfuscate-email.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("react"))&&"object"==typeof e&&"default"in e?e.default:e;exports.Thing=function(e){return t.createElement("div",null,e.children||"the snozzberries taste like snozzberries")}; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var n=require("react"),t=e(n),r=e(require("styled-components"));function a(e,n){return n||(n=e.slice(0)),e.raw=n,e}function o(e){var n=e[1];return"string"==typeof n&&n.length>0}var c,u=["body","subject","children","className","email"],i=r.span(c||(c=a(["\n &::after {\n content: '@';\n }\n"])));exports.Email=function(e){var r=e.body,a=void 0===r?"":r,c=e.subject,l=void 0===c?"":c,s=e.children,f=e.className,d=e.email,m=function(e,n){if(null==e)return{};var t,r,a={},o=Object.keys(e);for(r=0;r<o.length;r++)n.indexOf(t=o[r])>=0||(a[t]=e[t]);return a}(e,u),b=n.useState(!1),v=b[0],p=b[1],j=new URL("mailto:"+d);function y(){p(!0)}j.search=Object.entries({body:a,subject:l}).filter(o).map((function(e){return e[0]+"="+encodeURIComponent(e[1])})).join("&");var h=s||d,O=s||function(e){var n=e.split("@"),r=n[1];return t.createElement(t.Fragment,null,n[0],t.createElement(i,null),r)}(d);return t.createElement("a",Object.assign({className:f,href:v?j.href:"#",onFocus:y,onMouseOver:y},m),v?h:O)}; | ||
//# sourceMappingURL=react-obfuscate-email.cjs.production.min.js.map |
@@ -1,15 +0,102 @@ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import styled from 'styled-components'; | ||
// see: https://github.com/storybookjs/storybook/issues/9556 | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
/** | ||
* A custom Thing component. Neat! | ||
*/ | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
var Thing = function Thing(_ref) { | ||
var children = _ref.children; | ||
return React.createElement("div", null, children || "the snozzberries taste like snozzberries"); | ||
}; | ||
return target; | ||
} | ||
export { Thing }; | ||
function _taggedTemplateLiteralLoose(strings, raw) { | ||
if (!raw) { | ||
raw = strings.slice(0); | ||
} | ||
strings.raw = raw; | ||
return strings; | ||
} | ||
function isValidParam(param) { | ||
var value = param[1]; | ||
return typeof value === 'string' && value.length > 0; | ||
} | ||
/* | ||
Do not use URLSearchParams. | ||
URLSearchParams turns spaces into '+': | ||
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#examples | ||
Some email clients on mobile apps will render the '+' and not properly replace it with spaces. | ||
For the 'mailto' protocol, spaces should be percent encoded according to the spec: | ||
https://datatracker.ietf.org/doc/html/rfc6068#ref-STD66 | ||
*/ | ||
function percentEncodeParams(params) { | ||
return Object.entries(params).filter(isValidParam).map(function (_ref) { | ||
var key = _ref[0], | ||
value = _ref[1]; | ||
return key + "=" + encodeURIComponent(value); | ||
}).join('&'); | ||
} | ||
var _excluded = ["body", "subject", "children", "className", "email"]; | ||
var _templateObject; | ||
var Obfuscated = /*#__PURE__*/styled.span(_templateObject || (_templateObject = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n &::after {\n content: '@';\n }\n"]))); | ||
function obfuscateEmail(email) { | ||
var _email$split = email.split('@'), | ||
username = _email$split[0], | ||
domain = _email$split[1]; | ||
return React.createElement(React.Fragment, null, username, React.createElement(Obfuscated, null), domain); | ||
} | ||
function Email(_ref) { | ||
var _ref$body = _ref.body, | ||
body = _ref$body === void 0 ? '' : _ref$body, | ||
_ref$subject = _ref.subject, | ||
subject = _ref$subject === void 0 ? '' : _ref$subject, | ||
children = _ref.children, | ||
className = _ref.className, | ||
email = _ref.email, | ||
rest = _objectWithoutPropertiesLoose(_ref, _excluded); | ||
var _useState = useState(false), | ||
hovered = _useState[0], | ||
setHovered = _useState[1]; | ||
var emailUrl = new URL("mailto:" + email); // https://github.com/whatwg/url/issues/18#issuecomment-369865339 | ||
emailUrl.search = percentEncodeParams({ | ||
body: body, | ||
subject: subject | ||
}); | ||
function handleHover() { | ||
setHovered(true); | ||
} | ||
var displayText = children || email; | ||
var obfuscatedText = children || obfuscateEmail(email); | ||
return React.createElement("a", Object.assign({ | ||
className: className, | ||
href: hovered ? emailUrl.href : '#', | ||
onFocus: handleHover, | ||
onMouseOver: handleHover | ||
}, rest), hovered ? displayText : obfuscatedText); | ||
} | ||
export { Email }; | ||
//# sourceMappingURL=react-obfuscate-email.esm.js.map |
{ | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"license": "MIT", | ||
@@ -25,3 +25,4 @@ "main": "dist/index.js", | ||
"peerDependencies": { | ||
"react": ">=16" | ||
"react": ">=16", | ||
"styled-components": ">=5" | ||
}, | ||
@@ -62,2 +63,3 @@ "husky": { | ||
"@types/react-dom": "^17.0.9", | ||
"@types/styled-components": "^5.1.12", | ||
"babel-loader": "^8.2.2", | ||
@@ -69,2 +71,3 @@ "husky": "^7.0.1", | ||
"size-limit": "^5.0.3", | ||
"styled-components": "^5.3.0", | ||
"tsdx": "^0.14.1", | ||
@@ -71,0 +74,0 @@ "tslib": "^2.3.1", |
188
README.md
@@ -1,181 +0,33 @@ | ||
# TSDX React w/ Storybook User Guide | ||
# React Obfuscate Email | ||
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it. | ||
📧 React component to obfuscate email links | ||
> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`. | ||
> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/) | ||
## Commands | ||
TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`. | ||
The recommended workflow is to run TSDX in one terminal: | ||
```bash | ||
npm start # or yarn start | ||
``` | ||
import { Email } from "react-obfuscate-email"; | ||
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`. | ||
Then run either Storybook or the example playground: | ||
### Storybook | ||
Run inside another terminal: | ||
```bash | ||
yarn storybook | ||
export default function SomeComponent() { | ||
return ( | ||
<Email email="test@example.com">📧 Email me!</Email> | ||
) | ||
} | ||
``` | ||
This loads the stories from `./stories`. | ||
If no children is given, it will use the email as the displayed text for the link. | ||
> NOTE: Stories should reference the components as if using the library, similar to the example playground. This means importing from the root project directory. This has been aliased in the tsconfig and the storybook webpack config as a helper. | ||
It also accepts `body` and `subject` props that will be use on the link: | ||
### Example | ||
Then run the example inside another: | ||
```bash | ||
cd example | ||
npm i # or yarn to install dependencies | ||
npm start # or yarn start | ||
``` | ||
import { Email } from "react-obfuscate-email"; | ||
The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. **No symlinking required**, we use [Parcel's aliasing](https://parceljs.org/module_resolution.html#aliases). | ||
To do a one-off build, use `npm run build` or `yarn build`. | ||
To run tests, use `npm test` or `yarn test`. | ||
## Configuration | ||
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly. | ||
### Jest | ||
Jest tests are set up to run with `npm test` or `yarn test`. | ||
### Bundle analysis | ||
Calculates the real cost of your library using [size-limit](https://github.com/ai/size-limit) with `npm run size` and visulize it with `npm run analyze`. | ||
#### Setup Files | ||
This is the folder structure we set up for you: | ||
```txt | ||
/example | ||
index.html | ||
index.tsx # test your component here in a demo app | ||
package.json | ||
tsconfig.json | ||
/src | ||
index.tsx # EDIT THIS | ||
/test | ||
blah.test.tsx # EDIT THIS | ||
/stories | ||
Thing.stories.tsx # EDIT THIS | ||
/.storybook | ||
main.js | ||
preview.js | ||
.gitignore | ||
package.json | ||
README.md # EDIT THIS | ||
tsconfig.json | ||
``` | ||
#### React Testing Library | ||
We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this. | ||
### Rollup | ||
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details. | ||
### TypeScript | ||
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs. | ||
## Continuous Integration | ||
### GitHub Actions | ||
Two actions are added by default: | ||
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix | ||
- `size` which comments cost comparison of your library on every pull request using [size-limit](https://github.com/ai/size-limit) | ||
## Optimizations | ||
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations: | ||
```js | ||
// ./types/index.d.ts | ||
declare var __DEV__: boolean; | ||
// inside your code... | ||
if (__DEV__) { | ||
console.log('foo'); | ||
export default function SomeComponent() { | ||
return ( | ||
email="test@example.com" | ||
body="You rock! 🚀" | ||
subject="Hello, world! 👋' | ||
/> | ||
) | ||
} | ||
``` | ||
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions. | ||
## Module Formats | ||
CJS, ESModules, and UMD module formats are supported. | ||
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found. | ||
## Deploying the Example Playground | ||
The Playground is just a simple [Parcel](https://parceljs.org) app, you can deploy it anywhere you would normally deploy that. Here are some guidelines for **manually** deploying with the Netlify CLI (`npm i -g netlify-cli`): | ||
```bash | ||
cd example # if not already in the example folder | ||
npm run build # builds to dist | ||
netlify deploy # deploy the dist folder | ||
``` | ||
Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify: | ||
```bash | ||
netlify init | ||
# build command: yarn build && cd example && yarn && yarn build | ||
# directory to deploy: example/dist | ||
# pick yes for netlify.toml | ||
``` | ||
## Named Exports | ||
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library. | ||
## Including Styles | ||
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like. | ||
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader. | ||
## Publishing to NPM | ||
We recommend using [np](https://github.com/sindresorhus/np). | ||
## Usage with Lerna | ||
When creating a new package with TSDX within a project set up with Lerna, you might encounter a `Cannot resolve dependency` error when trying to run the `example` project. To fix that you will need to make changes to the `package.json` file _inside the `example` directory_. | ||
The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's `package.json` might not point to the right place, as those dependencies might have been installed in the root of your Lerna project. | ||
Change the `alias` to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below. | ||
```diff | ||
"alias": { | ||
- "react": "../node_modules/react", | ||
- "react-dom": "../node_modules/react-dom" | ||
+ "react": "../../../node_modules/react", | ||
+ "react-dom": "../../../node_modules/react-dom" | ||
}, | ||
``` | ||
An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. [However, that might cause other problems.](https://github.com/palmerhq/tsdx/issues/64) | ||
It uses `styled-components`, it should be installed on your project. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
25523
14
273
2
2
20
3
34
1