@weser/layers 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-present Robin Weser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # @weser/layers
2
+
3
+ <img alt="npm version" src="https://badge.fury.io/js/@weser/layers.svg"> <img alt="npm downloads" src="https://img.shields.io/npm/dm/@weser/layers.svg"> <a href="https://bundlephobia.com/result?p=@weser/layers@latest"><img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@weser/layers.svg"></a>
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ # npm
9
+ npm i --save @weser/layers
10
+ # yarn
11
+ yarn add @weser/layers
12
+ # pnpm
13
+ pnpm add @weser/layers
14
+ ```
15
+
16
+ ## Documentation
17
+
18
+ ## License
19
+
20
+ @weser/layers is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br>
21
+ Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br>
22
+ Created with ♥ by [@robinweser](http://weser.io).
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ export type T_Layer = {
3
+ id: string;
4
+ element: Object;
5
+ };
6
+ type LayerContext = {
7
+ layers: Array<T_Layer>;
8
+ addLayer: (layer: T_Layer) => void;
9
+ removeLayer: (id: T_Layer['id']) => void;
10
+ hasLayer: (id: T_Layer['id']) => boolean;
11
+ };
12
+ declare const _default: import("react").Context<LayerContext | null>;
13
+ export default _default;
@@ -0,0 +1,3 @@
1
+ 'use client';
2
+ import { createContext } from 'react';
3
+ export default createContext(null);
@@ -0,0 +1,8 @@
1
+ import { PropsWithChildren } from 'react';
2
+ import { T_Layer } from './LayerContext.js';
3
+ type Props = {
4
+ onLayerAdded?: (layers: Array<T_Layer>) => void;
5
+ onLayerRemoved?: (layers: Array<T_Layer>) => void;
6
+ };
7
+ export default function LayerContextProvider({ children, onLayerAdded, onLayerRemoved, }: PropsWithChildren<Props>): import("react").JSX.Element;
8
+ export {};
@@ -0,0 +1,36 @@
1
+ 'use client';
2
+ import { useState } from 'react';
3
+ import LayerContext from './LayerContext.js';
4
+ export default function LayerContextProvider({ children, onLayerAdded, onLayerRemoved, }) {
5
+ const [layers, setLayers] = useState([]);
6
+ function addLayer(layer) {
7
+ setLayers((prevLayers) => {
8
+ const filteredLayers = prevLayers.filter((item) => item.id !== layer.id);
9
+ const newLayers = [...filteredLayers, layer];
10
+ if (onLayerAdded) {
11
+ onLayerAdded(newLayers);
12
+ }
13
+ return newLayers;
14
+ });
15
+ }
16
+ function removeLayer(id) {
17
+ setLayers((prevLayers) => {
18
+ const newLayers = prevLayers.filter((item) => item.id !== id);
19
+ if (onLayerRemoved) {
20
+ onLayerRemoved(newLayers);
21
+ }
22
+ return newLayers;
23
+ });
24
+ }
25
+ function hasLayer(id) {
26
+ return layers.find((item) => item.id === id) !== undefined;
27
+ }
28
+ return (<LayerContext.Provider value={{
29
+ addLayer,
30
+ removeLayer,
31
+ hasLayer,
32
+ layers,
33
+ }}>
34
+ {children}
35
+ </LayerContext.Provider>);
36
+ }
@@ -0,0 +1,4 @@
1
+ export { default as LayerContext, T_Layer } from './LayerContext.js';
2
+ export { default as LayerContextProvider } from './LayerContextProvider.jsx';
3
+ export { default as useLayerContext } from './useLayerContext.js';
4
+ export { default as useLayer } from './useLayer.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { default as LayerContext } from './LayerContext.js';
2
+ export { default as LayerContextProvider } from './LayerContextProvider.jsx';
3
+ export { default as useLayerContext } from './useLayerContext.js';
4
+ export { default as useLayer } from './useLayer.js';
@@ -0,0 +1 @@
1
+ export default function useClientSide(): boolean;
@@ -0,0 +1,6 @@
1
+ import { useEffect, useState } from 'react';
2
+ export default function useClientSide() {
3
+ const [isClientSide, setClientSide] = useState(false);
4
+ useEffect(() => setClientSide(true), []);
5
+ return isClientSide;
6
+ }
@@ -0,0 +1,2 @@
1
+ import { RefObject } from 'react';
2
+ export default function useLayer<E extends HTMLElement>(visible: boolean): [RefObject<E>, boolean];
@@ -0,0 +1,20 @@
1
+ 'use client';
2
+ import { useEffect, useId, useRef } from 'react';
3
+ import useLayerContext from './useLayerContext.js';
4
+ export default function useLayer(visible) {
5
+ const id = useId();
6
+ const ref = useRef(null);
7
+ const { layers, addLayer, removeLayer, hasLayer } = useLayerContext();
8
+ useEffect(() => {
9
+ if (visible) {
10
+ addLayer({ id, element: ref });
11
+ }
12
+ else {
13
+ if (hasLayer(id)) {
14
+ removeLayer(id);
15
+ }
16
+ }
17
+ }, [visible]);
18
+ const active = layers[layers.length - 1]?.id === id;
19
+ return [ref, active];
20
+ }
@@ -0,0 +1,6 @@
1
+ export default function useLayerContext(): {
2
+ layers: import("./LayerContext.js").T_Layer[];
3
+ addLayer: (layer: import("./LayerContext.js").T_Layer) => void;
4
+ removeLayer: (id: string) => void;
5
+ hasLayer: (id: string) => boolean;
6
+ };
@@ -0,0 +1,10 @@
1
+ 'use client';
2
+ import { useContext } from 'react';
3
+ import LayerContext from './LayerContext.js';
4
+ export default function useLayerContext() {
5
+ const context = useContext(LayerContext);
6
+ if (!context) {
7
+ throw new Error("You're using layer context outside of its provider.");
8
+ }
9
+ return context;
10
+ }
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@weser/layers",
3
+ "version": "0.0.1",
4
+ "description": "React library for working with multiple layers",
5
+ "author": "Robin Weser <robin@weser.io>",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/robinweser/weser.git",
8
+ "repository": "https://github.com/robinweser/weser.git",
9
+ "type": "module",
10
+ "main": "dist/index.js",
11
+ "module": "dist/index.js",
12
+ "types": "dist/index.d.ts",
13
+ "sideEffects": false,
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "LICENSE",
19
+ "README.md",
20
+ "dist/**"
21
+ ],
22
+ "browserslist": [
23
+ "IE >= 11",
24
+ "Firefox >= 60",
25
+ "Safari >= 11.1",
26
+ "Chrome >= 66",
27
+ "ChromeAndroid >= 66",
28
+ "iOS >= 11.3",
29
+ "Edge >= 15"
30
+ ],
31
+ "scripts": {
32
+ "setup": "pnpm build",
33
+ "clean": "rimraf dist",
34
+ "build": "tsc -b",
35
+ "dev": "pnpm build -w",
36
+ "test": "echo 1"
37
+ },
38
+ "keywords": [
39
+ "react",
40
+ "react-hook",
41
+ "react hooks",
42
+ "hooks",
43
+ "utils",
44
+ "layers",
45
+ "ui layers"
46
+ ],
47
+ "peerDependencies": {
48
+ "react": ">16.3.0"
49
+ },
50
+ "devDependencies": {
51
+ "@types/react": "^18.3.3",
52
+ "ava": "^6.1.3",
53
+ "react": "canary",
54
+ "rimraf": "^3.0.2",
55
+ "typescript": "^5.4.5"
56
+ },
57
+ "gitHead": "f8cd929768ab80c7c441f66770df2a95c33318a7"
58
+ }