@mbrain/epic-react-native-lib 0.0.3 → 0.0.4

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/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var S=Object.create;var s=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var m=Object.getPrototypeOf,y=Object.prototype.hasOwnProperty;var k=(t,e)=>{for(var n in e)s(t,n,{get:e[n],enumerable:!0})},u=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of x(e))!y.call(t,i)&&i!==n&&s(t,i,{get:()=>e[i],enumerable:!(o=l(e,i))||o.enumerable});return t};var d=(t,e,n)=>(n=t!=null?S(m(t)):{},u(e||!t||!t.__esModule?s(n,"default",{value:t,enumerable:!0}):n,t)),L=t=>u(s({},"__esModule",{value:!0}),t);var P={};k(P,{Box1:()=>p,useOrientation:()=>O});module.exports=L(P);var c=d(require("react"),1),r=require("react-native");function p({style:t,children:e}){let[n,o]=(0,c.useState)(0);return c.default.createElement(r.View,{style:t},c.default.createElement(r.Text,{style:g.text,onPress:()=>{o(i=>i+1)}},"Box1 ",n),e)}var g=r.StyleSheet.create({text:{fontSize:20,padding:15,borderRadius:10,color:"#ffffff",backgroundColor:"#0a234a"}});var f=require("react"),a=d(require("expo-screen-orientation"),1);function O(){let[t,e]=(0,f.useState)({orientation:void 0,orientationLock:void 0});return(0,f.useEffect)(()=>{Promise.all([a.getOrientationAsync(),a.getOrientationLockAsync()]).then(([o,i])=>{e({orientation:o,orientationLock:i})});let n=a.addOrientationChangeListener(o=>{e({orientation:o.orientationInfo.orientation,orientationLock:o.orientationLock})});return()=>n.remove()},[]),t}
1
+ "use strict";var p=Object.create;var y=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var V=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var v=(t,e)=>{for(var o in e)y(t,o,{get:e[o],enumerable:!0})},f=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of O(e))!w.call(t,a)&&a!==o&&y(t,a,{get:()=>e[a],enumerable:!(r=k(e,a))||r.enumerable});return t};var d=(t,e,o)=>(o=t!=null?p(V(t)):{},f(e||!t||!t.__esModule?y(o,"default",{value:t,enumerable:!0}):o,t)),M=t=>f(y({},"__esModule",{value:!0}),t);var C={};v(C,{Box1:()=>g,createLocalStorage:()=>K,useOrientation:()=>h});module.exports=M(C);var l=d(require("react"),1),i=require("react-native");function g({style:t,children:e}){let[o,r]=(0,l.useState)(0);return l.default.createElement(i.View,{style:t},l.default.createElement(i.Text,{style:A.text,onPress:()=>{r(a=>a+1)}},"Box1 ",o),e)}var A=i.StyleSheet.create({text:{fontSize:20,padding:15,borderRadius:10,color:"#ffffff",backgroundColor:"#0a234a"}});var m=require("react"),s=d(require("expo-screen-orientation"),1);function h(){let[t,e]=(0,m.useState)({orientation:void 0,orientationLock:void 0});return(0,m.useEffect)(()=>{Promise.all([s.getOrientationAsync(),s.getOrientationLockAsync()]).then(([r,a])=>{e({orientation:r,orientationLock:a})});let o=s.addOrientationChangeListener(r=>{e({orientation:r.orientationInfo.orientation,orientationLock:r.orientationLock})});return()=>o.remove()},[]),t}var L=require("react"),u=require("react-native-mmkv");function K({defaults:t,storage:e=(0,u.createMMKV)()}){let o=n=>{try{let c=e.getString(String(n));return c!==void 0?JSON.parse(c):t[n]}catch(c){return console.warn(c),t[n]}};return{useLocalStorage:n=>{let[c,S]=(0,L.useState)(()=>o(n));return(0,u.useMMKVListener)(x=>{String(n)===x&&S(o(n))},e),c},setLocalStorage:(n,c)=>{try{let S=JSON.stringify(c);e.set(String(n),S)}catch(S){console.warn(S)}},getLocalStorage:n=>o(n),removeLocalStorage:n=>{try{e.remove(String(n))}catch(c){console.warn(c)}},clearAllLocalStorage:()=>{try{e.clearAll()}catch(n){console.warn(n)}},storage:e}}
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import a,{useState as c}from"react";import{View as s,Text as f,StyleSheet as u}from"react-native";function d({style:o,children:n}){let[i,t]=c(0);return a.createElement(s,{style:o},a.createElement(f,{style:p.text,onPress:()=>{t(r=>r+1)}},"Box1 ",i),n)}var p=u.create({text:{fontSize:20,padding:15,borderRadius:10,color:"#ffffff",backgroundColor:"#0a234a"}});import{useEffect as O,useState as S}from"react";import*as e from"expo-screen-orientation";function l(){let[o,n]=S({orientation:void 0,orientationLock:void 0});return O(()=>{Promise.all([e.getOrientationAsync(),e.getOrientationLockAsync()]).then(([t,r])=>{n({orientation:t,orientationLock:r})});let i=e.addOrientationChangeListener(t=>{n({orientation:t.orientationInfo.orientation,orientationLock:t.orientationLock})});return()=>i.remove()},[]),o}export{d as Box1,l as useOrientation};
1
+ import S,{useState as y}from"react";import{View as m,Text as u,StyleSheet as f}from"react-native";function d({style:a,children:t}){let[n,r]=y(0);return S.createElement(m,{style:a},S.createElement(u,{style:g.text,onPress:()=>{r(s=>s+1)}},"Box1 ",n),t)}var g=f.create({text:{fontSize:20,padding:15,borderRadius:10,color:"#ffffff",backgroundColor:"#0a234a"}});import{useEffect as h,useState as L}from"react";import*as c from"expo-screen-orientation";function K(){let[a,t]=L({orientation:void 0,orientationLock:void 0});return h(()=>{Promise.all([c.getOrientationAsync(),c.getOrientationLockAsync()]).then(([r,s])=>{t({orientation:r,orientationLock:s})});let n=c.addOrientationChangeListener(r=>{t({orientation:r.orientationInfo.orientation,orientationLock:r.orientationLock})});return()=>n.remove()},[]),a}import{useState as x}from"react";import{createMMKV as p,useMMKVListener as k}from"react-native-mmkv";function O({defaults:a,storage:t=p()}){let n=e=>{try{let o=t.getString(String(e));return o!==void 0?JSON.parse(o):a[e]}catch(o){return console.warn(o),a[e]}};return{useLocalStorage:e=>{let[o,i]=x(()=>n(e));return k(l=>{String(e)===l&&i(n(e))},t),o},setLocalStorage:(e,o)=>{try{let i=JSON.stringify(o);t.set(String(e),i)}catch(i){console.warn(i)}},getLocalStorage:e=>n(e),removeLocalStorage:e=>{try{t.remove(String(e))}catch(o){console.warn(o)}},clearAllLocalStorage:()=>{try{t.clearAll()}catch(e){console.warn(e)}},storage:t}}export{d as Box1,O as createLocalStorage,K as useOrientation};
@@ -1,2 +1,3 @@
1
1
  export { Box1 } from './components/Box1';
2
2
  export { useOrientation } from './utils/orientation';
3
+ export { createLocalStorage } from './storage/local-storage';
@@ -0,0 +1,56 @@
1
+ import { MMKV } from 'react-native-mmkv';
2
+ type LocalStorageClient<Schema extends Record<string, unknown>> = {
3
+ useLocalStorage<Key extends keyof Schema>(key: Key): Schema[Key];
4
+ setLocalStorage<Key extends keyof Schema>(key: Key, value: Schema[Key]): void;
5
+ getLocalStorage<Key extends keyof Schema>(key: Key): Schema[Key];
6
+ removeLocalStorage(key: keyof Schema): void;
7
+ clearAllLocalStorage(): void;
8
+ storage: MMKV;
9
+ };
10
+ type LocalStorageOptions<Schema extends Record<string, unknown>> = {
11
+ defaults: Schema;
12
+ storage?: MMKV;
13
+ };
14
+ /**
15
+ * Generates a type-safe wrapper around MMKV that provides hooks and utilities
16
+ * for synchronizing persisted values with React state.
17
+ *
18
+ * ## Usage
19
+ *
20
+ * Create config file: src/config/local-storage.ts
21
+ *
22
+ * ```
23
+ * import { createMMKV } from 'react-native-mmkv';
24
+ * import { createLocalStorage } from '@mbrain/epic-react-native-lib';
25
+ *
26
+ * export enum LocalStorageKey {
27
+ * KEY1 = 'key1',
28
+ * KEY2 = 'key2',
29
+ * }
30
+ *
31
+ * type LocalStorageType = {
32
+ * [LocalStorageKey.KEY1]: string;
33
+ * [LocalStorageKey.KEY2]: number;
34
+ * };
35
+ *
36
+ * const LOCAL_STORAGE_DEFAULT: LocalStorageType = {
37
+ * [LocalStorageKey.KEY1]: 'Hello',
38
+ * [LocalStorageKey.KEY2]: 42,
39
+ * };
40
+ *
41
+ * export const mmkv = createMMKV();
42
+ *
43
+ * const localStorage = createLocalStorage<LocalStorageType>({
44
+ * defaults: LOCAL_STORAGE_DEFAULT,
45
+ * storage: mmkv,
46
+ * });
47
+ *
48
+ * export const useLocalStorage = localStorage.useLocalStorage;
49
+ * export const setLocalStorage = localStorage.setLocalStorage;
50
+ * export const getLocalStorage = localStorage.getLocalStorage;
51
+ * export const removeLocalStorage = localStorage.removeLocalStorage;
52
+ * export const clearAllLocalStorage = localStorage.clearAllLocalStorage;
53
+ * ```
54
+ */
55
+ export declare function createLocalStorage<Schema extends Record<string, unknown>>({ defaults, storage, }: LocalStorageOptions<Schema>): LocalStorageClient<Schema>;
56
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mbrain/epic-react-native-lib",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "A set of helpful, battle-tested utilities that simplify common React Native workflows and speed up app development",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -34,7 +34,8 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "expo-screen-orientation": "^9.0.7"
37
+ "expo-screen-orientation": "^9.0.0",
38
+ "react-native-mmkv": "^4.0.1"
38
39
  },
39
40
  "peerDependencies": {
40
41
  "react": ">=18.0.0",