|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2017-present DaniAkash |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import { useState, useEffect, useRef } from "react"; |
| 26 | +import { Dimensions, ScaledSize } from "react-native"; |
| 27 | + |
| 28 | +const useDimensionsListener = () => { |
| 29 | + const [screenDimension, setScreenDimension] = useState( |
| 30 | + Dimensions.get("screen") |
| 31 | + ); |
| 32 | + const [windowDimension, setWindowDimension] = useState( |
| 33 | + Dimensions.get("window") |
| 34 | + ); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + function handleDimensionChange({ |
| 38 | + window, |
| 39 | + screen |
| 40 | + }: { |
| 41 | + window: ScaledSize; |
| 42 | + screen: ScaledSize; |
| 43 | + }) { |
| 44 | + setWindowDimension(window); |
| 45 | + setScreenDimension(screen); |
| 46 | + } |
| 47 | + |
| 48 | + Dimensions.addEventListener("change", handleDimensionChange); |
| 49 | + return () => { |
| 50 | + Dimensions.removeEventListener("change", handleDimensionChange); |
| 51 | + }; |
| 52 | + }, []); |
| 53 | + |
| 54 | + return { |
| 55 | + screen: screenDimension, |
| 56 | + window: windowDimension |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +type EffectParams = { |
| 61 | + screen: ScaledSize; |
| 62 | + window: ScaledSize; |
| 63 | +}; |
| 64 | + |
| 65 | +type EffectCallback = |
| 66 | + | ((opts: EffectParams) => () => any) |
| 67 | + | ((opts: EffectParams) => undefined) |
| 68 | + | ((opts: EffectParams) => void); |
| 69 | + |
| 70 | +export const useDimensionsChange = (effect: EffectCallback) => { |
| 71 | + const hasMountRef = useRef(false); |
| 72 | + const dimensions = useDimensionsListener(); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + if (hasMountRef.current) { |
| 76 | + const destroy = effect(dimensions); |
| 77 | + let cleanUp: any = () => null; |
| 78 | + if (typeof destroy === "function") { |
| 79 | + cleanUp = destroy; |
| 80 | + } |
| 81 | + return () => cleanUp(); |
| 82 | + } else { |
| 83 | + hasMountRef.current = true; |
| 84 | + } |
| 85 | + }, [dimensions, hasMountRef]); |
| 86 | +}; |
| 87 | + |
| 88 | +export const responsiveHeight = (h: number) => { |
| 89 | + const { height } = Dimensions.get("window"); |
| 90 | + return height * (h / 100); |
| 91 | +}; |
| 92 | + |
| 93 | +export const responsiveWidth = (w: number) => { |
| 94 | + const { width } = Dimensions.get("window"); |
| 95 | + return width * (w / 100); |
| 96 | +}; |
| 97 | + |
| 98 | +export const responsiveFontSize = (f: number) => { |
| 99 | + const { height, width } = Dimensions.get("window"); |
| 100 | + const lowerSize = height > width ? width : height; |
| 101 | + const tempHeight = (16 / 9) * lowerSize; |
| 102 | + return ( |
| 103 | + Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100) |
| 104 | + ); |
| 105 | +}; |
| 106 | + |
| 107 | +export const responsiveScreenHeight = (h: number) => { |
| 108 | + const { height } = Dimensions.get("screen"); |
| 109 | + return height * (h / 100); |
| 110 | +}; |
| 111 | + |
| 112 | +export const responsiveScreenWidth = (w: number) => { |
| 113 | + const { width } = Dimensions.get("screen"); |
| 114 | + return width * (w / 100); |
| 115 | +}; |
| 116 | + |
| 117 | +export const responsiveScreenFontSize = (f: number) => { |
| 118 | + const { height, width } = Dimensions.get("screen"); |
| 119 | + const lowerSize = height > width ? width : height; |
| 120 | + const tempHeight = (16 / 9) * lowerSize; |
| 121 | + return ( |
| 122 | + Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100) |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +export const useResponsiveHeight = (h: number) => { |
| 127 | + const { height } = useDimensionsListener().window; |
| 128 | + return height * (h / 100); |
| 129 | +}; |
| 130 | + |
| 131 | +export const useResponsiveWidth = (w: number) => { |
| 132 | + const { width } = useDimensionsListener().window; |
| 133 | + return width * (w / 100); |
| 134 | +}; |
| 135 | + |
| 136 | +export const useResponsiveFontSize = (f: number) => { |
| 137 | + const { height, width } = useDimensionsListener().window; |
| 138 | + const lowerSize = height > width ? width : height; |
| 139 | + const tempHeight = (16 / 9) * lowerSize; |
| 140 | + return ( |
| 141 | + Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100) |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +export const useResponsiveScreenHeight = (h: number) => { |
| 146 | + const { height } = useDimensionsListener().screen; |
| 147 | + return height * (h / 100); |
| 148 | +}; |
| 149 | + |
| 150 | +export const useResponsiveScreenWidth = (w: number) => { |
| 151 | + const { width } = useDimensionsListener().screen; |
| 152 | + return width * (w / 100); |
| 153 | +}; |
| 154 | + |
| 155 | +export const useResponsiveScreenFontSize = (f: number) => { |
| 156 | + const { height, width } = useDimensionsListener().screen; |
| 157 | + const lowerSize = height > width ? width : height; |
| 158 | + const tempHeight = (16 / 9) * lowerSize; |
| 159 | + return ( |
| 160 | + Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100) |
| 161 | + ); |
| 162 | +}; |
0 commit comments