react-inlinesvg 3.0.2 → 3.1.0-0

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,8 +1,8 @@
1
1
  # react-inlinesvg
2
2
 
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)
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) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=gilbarbara_react-inlinesvg&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=gilbarbara_react-inlinesvg) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=gilbarbara_react-inlinesvg&metric=coverage)](https://sonarcloud.io/summary/new_code?id=gilbarbara_react-inlinesvg)
4
4
 
5
- Load inline, local or remote SVGs in your React components.
5
+ Load inline, local, or remote SVGs in your React components.
6
6
 
7
7
  View the [demo](https://codesandbox.io/s/github/gilbarbara/react-inlinesvg/tree/main/demo)
8
8
 
@@ -43,11 +43,11 @@ export default function App() {
43
43
  ## Props
44
44
 
45
45
  **src** {string} - **required**.
46
- The SVG file you want to load. It can be a require, URL or a string (base64 or url encoded).
47
- _If you are using create-react-app and your file resides in the `public` directory you can use the path directly without require._
46
+ The SVG file you want to load. It can be a require, URL, or a string (base64 or URL encoded).
47
+ _If you are using create-react-app and your file resides in the `public` directory, you can use the path directly without require._
48
48
 
49
49
  **baseURL** {string}
50
- An URL to prefix each ID in case you are using the `<base>` tag and `uniquifyIDs`.
50
+ An URL to prefix each ID in case you use the `<base>` tag and `uniquifyIDs`.
51
51
 
52
52
  **children** {ReactNode}
53
53
  The fallback content in case of a fetch error or unsupported browser.
@@ -100,13 +100,14 @@ This will receive a single argument with:
100
100
 
101
101
  **onLoad** {function}.
102
102
  A callback to be invoked upon successful load.
103
- This will receive 2 arguments: the `src` prop and a `hasCache` boolean
103
+ This will receive 2 arguments: the `src` prop and an `isCached` boolean
104
104
 
105
105
  **preProcessor** {function} ▶︎ `string`
106
106
  A function to process the contents of the SVG text before parsing.
107
107
 
108
- **title** {string}
109
- A title for your SVG. It will override an existing `<title>` tag.
108
+ **title** {string | null}
109
+ A title for your SVG. It will override an existing `<title>` tag.
110
+ If `null` is passed, the `<title>` tag will be removed.
110
111
 
111
112
  **uniqueHash** {string} ▶︎ a random 8 characters string `[A-Za-z0-9]`
112
113
  A string to use with `uniquifyIDs`.
@@ -125,7 +126,7 @@ Create unique IDs for each icon.
125
126
  description="The React logo"
126
127
  loader={<span>Loading...</span>}
127
128
  onError={(error) => console.log(error.message)}
128
- onLoad={(src, hasCache) => console.log(src, hasCache)}
129
+ onLoad={(src, isCached) => console.log(src, isCached)}
129
130
  preProcessor={(code) => code.replace(/fill=".*?"/g, 'fill="currentColor"')}
130
131
  src="https://cdn.svgporn.com/logos/react.svg"
131
132
  title="React"
@@ -137,8 +138,26 @@ Create unique IDs for each icon.
137
138
  ## Caching
138
139
 
139
140
  The internal cache is exported as `cacheStore` if you need to debug or pre-cache some files.
141
+ Since version 4.x, the `cacheStore` is an instance of the [Cache](src/cache.ts) class instead of a plain object.
140
142
  ⚠️ Use it at your own risk.
141
143
 
144
+ ### Persistent cache
145
+
146
+ Starting with version 4.x you can use browser's [cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to store the SVGs persistently.
147
+ To set it up, you need to wrap your app with the provider:
148
+
149
+ ```typescript
150
+ import { createRoot } from 'react-dom/client';
151
+ import CacheProvider from 'react-inlinesvg/provider';
152
+ import App from './App';
153
+
154
+ createRoot(document.getElementById('root')!).render(
155
+ <CacheProvider>
156
+ <App />
157
+ </CacheProvider>
158
+ );
159
+ ```
160
+
142
161
  ## Browser Support
143
162
 
144
163
  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.
@@ -147,12 +166,12 @@ If you need to support legacy browsers you'll need to include a polyfiil for `fe
147
166
 
148
167
  ## CORS
149
168
 
150
- If you are loading remote SVGs, you'll need to make sure it has [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) support.
169
+ If you are loading remote SVGs, you'll need to ensure it has [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) support.
151
170
 
152
- ## Why you need this package?
171
+ ## Why do you need this package?
153
172
 
154
- One of the reasons SVGs are awesome is because you can style them with CSS.
155
- Unfortunately, this winds up not being too useful in practice because the style element has to be in the same document. This leaves you with three bad options:
173
+ One of the reasons SVGs are awesome is that you can style them with CSS.
174
+ Unfortunately, this is not useful in practice because the style element has to be in the same document. This leaves you with three bad options:
156
175
 
157
176
  1. Embed the CSS in the SVG document
158
177
  - Can't use your CSS preprocessors (LESS, SASS)
@@ -0,0 +1,74 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as React from 'react';
3
+
4
+ declare const STATUS: {
5
+ readonly IDLE: "idle";
6
+ readonly LOADING: "loading";
7
+ readonly LOADED: "loaded";
8
+ readonly FAILED: "failed";
9
+ readonly READY: "ready";
10
+ readonly UNSUPPORTED: "unsupported";
11
+ };
12
+
13
+ type ErrorCallback = (error: Error | FetchError) => void;
14
+ type LoadCallback = (src: string, isCached: boolean) => void;
15
+ type PlainObject<T = unknown> = Record<string, T>;
16
+ type PreProcessorCallback = (code: string) => string;
17
+ interface Props extends Omit<React.SVGProps<SVGElement>, 'onLoad' | 'onError' | 'ref'> {
18
+ baseURL?: string;
19
+ cacheRequests?: boolean;
20
+ children?: React.ReactNode;
21
+ description?: string;
22
+ fetchOptions?: RequestInit;
23
+ innerRef?: React.Ref<SVGElement>;
24
+ loader?: React.ReactNode;
25
+ onError?: ErrorCallback;
26
+ onLoad?: LoadCallback;
27
+ preProcessor?: PreProcessorCallback;
28
+ src: string;
29
+ title?: string | null;
30
+ uniqueHash?: string;
31
+ uniquifyIDs?: boolean;
32
+ }
33
+ interface State {
34
+ content: string;
35
+ element: React.ReactNode;
36
+ isCached: boolean;
37
+ status: Status;
38
+ }
39
+ interface FetchError extends Error {
40
+ code: string;
41
+ errno: string;
42
+ message: string;
43
+ type: string;
44
+ }
45
+ type Status = (typeof STATUS)[keyof typeof STATUS];
46
+ interface StorageItem {
47
+ content: string;
48
+ status: Status;
49
+ }
50
+
51
+ declare class CacheStore {
52
+ private cacheApi;
53
+ private readonly cacheStore;
54
+ private readonly subscribers;
55
+ private readonly usePersistentCache;
56
+ isReady: boolean;
57
+ constructor();
58
+ onReady(callback: () => void): void;
59
+ get(url: string, fetchOptions?: RequestInit): Promise<string>;
60
+ set(url: string, data: StorageItem): void;
61
+ isCached(url: string): boolean;
62
+ private fetchAndAddToInternalCache;
63
+ private fetchAndAddToPersistentCache;
64
+ private handleLoading;
65
+ keys(): Array<string>;
66
+ data(): Array<Record<string, StorageItem>>;
67
+ delete(url: string): Promise<void>;
68
+ clear(): Promise<void>;
69
+ }
70
+
71
+ declare let cacheStore: CacheStore;
72
+ declare function InlineSVG(props: Props): string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null;
73
+
74
+ export { ErrorCallback, FetchError, LoadCallback, PlainObject, PreProcessorCallback, Props, State, Status, StorageItem, cacheStore, InlineSVG as default };
@@ -0,0 +1,74 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as React from 'react';
3
+
4
+ declare const STATUS: {
5
+ readonly IDLE: "idle";
6
+ readonly LOADING: "loading";
7
+ readonly LOADED: "loaded";
8
+ readonly FAILED: "failed";
9
+ readonly READY: "ready";
10
+ readonly UNSUPPORTED: "unsupported";
11
+ };
12
+
13
+ type ErrorCallback = (error: Error | FetchError) => void;
14
+ type LoadCallback = (src: string, isCached: boolean) => void;
15
+ type PlainObject<T = unknown> = Record<string, T>;
16
+ type PreProcessorCallback = (code: string) => string;
17
+ interface Props extends Omit<React.SVGProps<SVGElement>, 'onLoad' | 'onError' | 'ref'> {
18
+ baseURL?: string;
19
+ cacheRequests?: boolean;
20
+ children?: React.ReactNode;
21
+ description?: string;
22
+ fetchOptions?: RequestInit;
23
+ innerRef?: React.Ref<SVGElement>;
24
+ loader?: React.ReactNode;
25
+ onError?: ErrorCallback;
26
+ onLoad?: LoadCallback;
27
+ preProcessor?: PreProcessorCallback;
28
+ src: string;
29
+ title?: string | null;
30
+ uniqueHash?: string;
31
+ uniquifyIDs?: boolean;
32
+ }
33
+ interface State {
34
+ content: string;
35
+ element: React.ReactNode;
36
+ isCached: boolean;
37
+ status: Status;
38
+ }
39
+ interface FetchError extends Error {
40
+ code: string;
41
+ errno: string;
42
+ message: string;
43
+ type: string;
44
+ }
45
+ type Status = (typeof STATUS)[keyof typeof STATUS];
46
+ interface StorageItem {
47
+ content: string;
48
+ status: Status;
49
+ }
50
+
51
+ declare class CacheStore {
52
+ private cacheApi;
53
+ private readonly cacheStore;
54
+ private readonly subscribers;
55
+ private readonly usePersistentCache;
56
+ isReady: boolean;
57
+ constructor();
58
+ onReady(callback: () => void): void;
59
+ get(url: string, fetchOptions?: RequestInit): Promise<string>;
60
+ set(url: string, data: StorageItem): void;
61
+ isCached(url: string): boolean;
62
+ private fetchAndAddToInternalCache;
63
+ private fetchAndAddToPersistentCache;
64
+ private handleLoading;
65
+ keys(): Array<string>;
66
+ data(): Array<Record<string, StorageItem>>;
67
+ delete(url: string): Promise<void>;
68
+ clear(): Promise<void>;
69
+ }
70
+
71
+ declare let cacheStore: CacheStore;
72
+ declare function InlineSVG(props: Props): string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null;
73
+
74
+ export { ErrorCallback, FetchError, LoadCallback, PlainObject, PreProcessorCallback, Props, State, Status, StorageItem, cacheStore, InlineSVG as default };