react-inlinesvg 3.0.3 → 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
@@ -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,7 +100,7 @@ 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.
@@ -126,7 +126,7 @@ Create unique IDs for each icon.
126
126
  description="The React logo"
127
127
  loader={<span>Loading...</span>}
128
128
  onError={(error) => console.log(error.message)}
129
- onLoad={(src, hasCache) => console.log(src, hasCache)}
129
+ onLoad={(src, isCached) => console.log(src, isCached)}
130
130
  preProcessor={(code) => code.replace(/fill=".*?"/g, 'fill="currentColor"')}
131
131
  src="https://cdn.svgporn.com/logos/react.svg"
132
132
  title="React"
@@ -138,8 +138,26 @@ Create unique IDs for each icon.
138
138
  ## Caching
139
139
 
140
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.
141
142
  ⚠️ Use it at your own risk.
142
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
+
143
161
  ## Browser Support
144
162
 
145
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.
@@ -148,12 +166,12 @@ If you need to support legacy browsers you'll need to include a polyfiil for `fe
148
166
 
149
167
  ## CORS
150
168
 
151
- 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.
152
170
 
153
- ## Why you need this package?
171
+ ## Why do you need this package?
154
172
 
155
- One of the reasons SVGs are awesome is because you can style them with CSS.
156
- 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:
157
175
 
158
176
  1. Embed the CSS in the SVG document
159
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 };