react-inlinesvg 3.0.3 → 3.1.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 +27 -9
- package/dist/index.d.mts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +1321 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +474 -0
- package/dist/index.mjs.map +1 -0
- package/dist/provider.d.mts +13 -0
- package/dist/provider.d.ts +13 -0
- package/dist/provider.js +67 -0
- package/dist/provider.js.map +1 -0
- package/dist/provider.mjs +15 -0
- package/dist/provider.mjs.map +1 -0
- package/package.json +44 -17
- package/src/cache.ts +164 -0
- package/src/config.ts +11 -0
- package/src/helpers.ts +22 -9
- package/src/index.tsx +56 -78
- package/src/provider.tsx +23 -0
- package/src/types.ts +2 -2
- package/esm/helpers.d.ts +0 -17
- package/esm/helpers.js +0 -58
- package/esm/helpers.js.map +0 -1
- package/esm/index.d.ts +0 -28
- package/esm/index.js +0 -471
- package/esm/index.js.map +0 -1
- package/esm/types.d.ts +0 -39
- package/esm/types.js +0 -2
- package/esm/types.js.map +0 -1
- package/lib/helpers.d.ts +0 -17
- package/lib/helpers.js +0 -66
- package/lib/helpers.js.map +0 -1
- package/lib/index.d.ts +0 -28
- package/lib/index.js +0 -488
- package/lib/index.js.map +0 -1
- package/lib/types.d.ts +0 -39
- package/lib/types.js +0 -3
- package/lib/types.js.map +0 -1
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
|
|
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
|
|
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
|
|
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,
|
|
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
|
|
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
|
|
156
|
-
Unfortunately, this
|
|
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)
|
package/dist/index.d.mts
ADDED
|
@@ -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 };
|
package/dist/index.d.ts
ADDED
|
@@ -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 };
|