react-inlinesvg 2.2.2 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # react-inlinesvg
2
2
 
3
- [![NPM version](https://badge.fury.io/js/react-inlinesvg.svg)](https://www.npmjs.com/package/react-inlinesvg) [![Build Status](https://travis-ci.com/gilbarbara/react-inlinesvg.svg?branch=master)](https://travis-ci.com/gilbarbara/react-inlinesvg) [![Maintainability](https://api.codeclimate.com/v1/badges/c7e42fe511b80cc25760/maintainability)](https://codeclimate.com/github/gilbarbara/react-inlinesvg/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/c7e42fe511b80cc25760/test_coverage)](https://codeclimate.com/github/gilbarbara/react-inlinesvg/test_coverage)
3
+ [![NPM version](https://badge.fury.io/js/react-inlinesvg.svg)](https://www.npmjs.com/package/react-inlinesvg) [![CI](https://github.com/gilbarbara/react-inlinesvg/actions/workflows/main.yml/badge.svg)](https://github.com/gilbarbara/react-inlinesvg/actions/workflows/main.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/c7e42fe511b80cc25760/maintainability)](https://codeclimate.com/github/gilbarbara/react-inlinesvg/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/c7e42fe511b80cc25760/test_coverage)](https://codeclimate.com/github/gilbarbara/react-inlinesvg/test_coverage)
4
4
 
5
5
  Load inline, local or remote SVGs in your React components.
6
6
 
7
+ View the [demo](https://codesandbox.io/s/github/gilbarbara/react-inlinesvg/tree/main/demo)
8
+
7
9
  ## Highlights
8
10
 
9
11
  - 🏖 **Easy to use:** Just set the `src`
@@ -20,7 +22,7 @@ npm i react-inlinesvg
20
22
 
21
23
  And import it into your code:
22
24
 
23
- ```typescript
25
+ ```tsx
24
26
  import React, { useRef } from 'react';
25
27
  import SVG, { Props as SVGProps } from 'react-inlinesvg';
26
28
 
@@ -34,8 +36,8 @@ export function App() {
34
36
  return (
35
37
  <main>
36
38
  <SVG src={`${process.env.PUBLIC_URL}/menu.svg`} width={24} height="auto" title="Menu" />
37
- <Logo ref={logo} src={`${process.env.PUBLIC_URL}/logo.svg`} />
38
- </main>
39
+ <Logo ref={logo} src={`${process.env.PUBLIC_URL}/logo.svg`} />
40
+ </main>
39
41
  );
40
42
  }
41
43
  ```
@@ -64,6 +66,9 @@ Cache remote SVGs.
64
66
  **description** {string}
65
67
  A description for your SVG. It will override an existing `<desc>` tag.
66
68
 
69
+ **fetchOptions** {RequestInit}
70
+ Custom options for the [request](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
71
+
67
72
  **innerRef** {React.Ref}
68
73
  Set a ref in SVGElement.
69
74
 
@@ -76,7 +81,7 @@ This will receive a single argument with:
76
81
 
77
82
  - a `FetchError` with:
78
83
 
79
- ```js
84
+ ```typescript
80
85
  {
81
86
  message: string;
82
87
  type: string;
@@ -87,11 +92,11 @@ This will receive a single argument with:
87
92
 
88
93
  - or an `InlineSVGError`, which has the following properties:
89
94
 
90
- ```js
95
+ ```typescript
91
96
  {
92
- name: 'InlineSVGError',
93
- data?: object,
94
- message: string
97
+ name: 'InlineSVGError';
98
+ data: object; // optional
99
+ message: string;
95
100
  }
96
101
  ```
97
102
 
@@ -111,7 +116,7 @@ A string to use with `uniquifyIDs`.
111
116
  **uniquifyIDs** {boolean} ▶︎ `false`
112
117
  Create unique IDs for each icon.
113
118
 
114
- > Any additional props will be passed down to the SVG element.
119
+ > Any additional props will be passed down to the SVG element.
115
120
 
116
121
  ### Example
117
122
 
@@ -121,9 +126,9 @@ Create unique IDs for each icon.
121
126
  cacheRequests={true}
122
127
  description="The React logo"
123
128
  loader={<span>Loading...</span>}
124
- onError={error => console.log(error.message)}
129
+ onError={(error) => console.log(error.message)}
125
130
  onLoad={(src, hasCache) => console.log(src, hasCache)}
126
- preProcessor={code => code.replace(/fill=".*?"/g, 'fill="currentColor"')}
131
+ preProcessor={(code) => code.replace(/fill=".*?"/g, 'fill="currentColor"')}
127
132
  src="https://cdn.svgporn.com/logos/react.svg"
128
133
  title="React"
129
134
  uniqueHash="a1f8d1"
@@ -131,6 +136,11 @@ Create unique IDs for each icon.
131
136
  />
132
137
  ```
133
138
 
139
+ ## Caching
140
+
141
+ The internal cache is exported as `cacheStore` if you need to debug or pre-cache some files.
142
+ ⚠️ Use it at your own risk.
143
+
134
144
  ## Browser Support
135
145
 
136
146
  Any browsers that support inlining [SVGs](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg) and [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) will work.
@@ -165,7 +175,7 @@ But there's an alternative that sidesteps these issues: load the SVG with a GET
165
175
 
166
176
  ### Note
167
177
 
168
- The SVG [`<use>`][svg-use-external-source] element can be used to achieve something similar to this component. See [this article][use-article] for more information and [this table][use-support] for browser support and caveats.
178
+ The SVG [`<use>`](http://css-tricks.com/svg-use-external-source) element can be used to achieve something similar to this component. See [this article](http://taye.me/blog/svg/a-guide-to-svg-use-elements/) for more information and [this table](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use#Browser_compatibility) for browser support and caveats.
169
179
 
170
180
  ## Credits
171
181
 
package/esm/helpers.d.ts CHANGED
@@ -14,4 +14,4 @@ export declare function randomString(length: number): string;
14
14
  /**
15
15
  * Remove properties from an object
16
16
  */
17
- export declare function removeProperties<T extends PlainObject, K extends keyof T>(input: T, ...filter: K[]): Omit<T, K>;
17
+ export declare function omit<T extends PlainObject, K extends keyof T>(input: T, ...filter: K[]): Omit<T, K>;
package/esm/helpers.js CHANGED
@@ -20,17 +20,18 @@ export function supportsInlineSVG() {
20
20
  }
21
21
  var div = document.createElement('div');
22
22
  div.innerHTML = '<svg />';
23
- return !!div.firstChild && div.firstChild.namespaceURI === 'http://www.w3.org/2000/svg';
23
+ var svg = div.firstChild;
24
+ return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';
25
+ }
26
+ function randomCharacter(character) {
27
+ return character[Math.floor(Math.random() * character.length)];
24
28
  }
25
29
  export function randomString(length) {
26
30
  var letters = 'abcdefghijklmnopqrstuvwxyz';
27
31
  var numbers = '1234567890';
28
- var charset = "" + letters + letters.toUpperCase() + numbers;
29
- var randomCharacter = function (character) {
30
- return character[Math.floor(Math.random() * character.length)];
31
- };
32
+ var charset = "".concat(letters).concat(letters.toUpperCase()).concat(numbers);
32
33
  var R = '';
33
- for (var i = 0; i < length; i++) {
34
+ for (var index = 0; index < length; index++) {
34
35
  R += randomCharacter(charset);
35
36
  }
36
37
  return R;
@@ -38,7 +39,7 @@ export function randomString(length) {
38
39
  /**
39
40
  * Remove properties from an object
40
41
  */
41
- export function removeProperties(input) {
42
+ export function omit(input) {
42
43
  var filter = [];
43
44
  for (var _i = 1; _i < arguments.length; _i++) {
44
45
  filter[_i - 1] = arguments[_i];
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,IAAI,aAAa,EAAE,MAAM,OAAO,CAAC;AAInD,MAAM,CAAC,IAAM,MAAM,GAAG;IACpB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,WAAW,EAAE,aAAa;CAC3B,CAAC;AAEF,MAAM,UAAU,SAAS;IACvB,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,sBAAsB;IACpC,OAAO,iBAAiB,EAAE,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,KAAK,IAAI,CAAC;AACjF,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,0BAA0B;IAC1B,IAAI,CAAC,QAAQ,EAAE;QACb,OAAO,KAAK,CAAC;KACd;IAED,IAAM,GAAG,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC1C,GAAG,CAAC,SAAS,GAAG,SAAS,CAAC;IAC1B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M,EAAE,KAAK,EAAE,EAAE;QAC3C,CAAC,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;KAC/B;IAED,OAAO,CAAC,CAAC;AACX,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,IAAI,CAClB,KAAQ;IACR,gBAAc;SAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;QAAd,+BAAc;;IAEd,IAAM,MAAM,GAAQ,EAAE,CAAC;IAEvB,KAAK,IAAM,GAAG,IAAI,KAAK,EAAE;QACvB,0BAA0B;QAC1B,IAAI,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE;YACtC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAmB,CAAC,EAAE;gBACzC,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;aAC1B;SACF;KACF;IAED,OAAO,MAAoB,CAAC;AAC9B,CAAC"}
package/esm/index.d.ts CHANGED
@@ -1,25 +1,28 @@
1
1
  import * as React from 'react';
2
- import { Props, State } from './types';
2
+ import { Props, State, StorageItem } from './types';
3
+ export declare const cacheStore: {
4
+ [key: string]: StorageItem;
5
+ };
3
6
  export default class InlineSVG extends React.PureComponent<Props, State> {
4
- constructor(props: Props);
7
+ private isInitialized;
5
8
  private isActive;
6
9
  private readonly hash;
7
10
  static defaultProps: {
8
11
  cacheRequests: boolean;
9
12
  uniquifyIDs: boolean;
10
13
  };
14
+ constructor(props: Props);
11
15
  componentDidMount(): void;
12
- componentDidUpdate(prevProps: Props, prevState: State): void;
16
+ componentDidUpdate(previousProps: Props, previousState: State): void;
13
17
  componentWillUnmount(): void;
14
- private processSVG;
15
- private updateSVGAttributes;
16
18
  private getNode;
17
19
  private getElement;
18
- private load;
19
- private handleCacheQueue;
20
20
  private handleLoad;
21
21
  private handleError;
22
22
  private request;
23
+ private load;
24
+ private updateSVGAttributes;
25
+ private processSVG;
23
26
  render(): React.ReactNode;
24
27
  }
25
28
  export * from './types';