Skip to content

Commit 9efc881

Browse files
committed
feat: Add useSvgDimensions() hook to @bothrs/expo
1 parent 239fc7f commit 9efc881

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

packages/expo/example/App.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useMemo, useState } from 'react'
44
import styled from 'styled-components/native'
55
import Constants from 'expo-constants'
66
// Packages to test
7-
import { parseConstants, parseGradient, conditionalMarkup, Gradient } from '@bothrs/expo'
7+
import { parseConstants, parseGradient, conditionalMarkup, Gradient, useSvgDimensions } from '@bothrs/expo'
88

99
/* --- Constants ------------------------------------------------------------------------------- */
1010

@@ -23,6 +23,13 @@ export default function App() {
2323
// Vars
2424
const [orientation, setOrientation] = useState<'landscape' | 'portrait'>(getOrientation())
2525

26+
// Hooks
27+
const svgDimensions = useSvgDimensions({
28+
originalSvgWidth: 100,
29+
originalSvgHeight: 80,
30+
containerWidth: Dimensions.get('screen').width
31+
})
32+
2633
// -- Test parseConstants() --
2734

2835
const parsedConstants = useMemo(() => {
@@ -55,6 +62,9 @@ export default function App() {
5562
<StTitle>{'<Gradient/>'}</StTitle>
5663
<StGradient linearGradient={EXAMPLE_GRADIENT_STRING} />
5764
<StSpacer />
65+
<StTitle>{'useSvgDimensions()'}</StTitle>
66+
<Text>{JSON.stringify(svgDimensions, null, 4)}</Text>
67+
<StSpacer />
5868
<StatusBar style="auto" />
5969
</ScrollView>
6070
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useMemo } from 'react'
2+
import { Dimensions } from 'react-native'
3+
4+
/* --- Types ----------------------------------------------------------------------------------- */
5+
6+
type UseSvgDimensionsInput = {
7+
originalSvgWidth: number
8+
originalSvgHeight: number
9+
containerWidth?: number
10+
}
11+
12+
/** --- useSvgDimensions() --------------------------------------------------------------------- */
13+
/** -i- Auto calculate the width, height & viewBox properties */
14+
export const useSvgDimensions = ({
15+
originalSvgHeight,
16+
originalSvgWidth,
17+
containerWidth = Dimensions.get('screen').width,
18+
}: UseSvgDimensionsInput) => {
19+
return useMemo(() => {
20+
const svgRatio = originalSvgHeight / originalSvgWidth
21+
return {
22+
width: containerWidth,
23+
height: containerWidth * svgRatio,
24+
viewBox: `0 0 ${originalSvgWidth} ${originalSvgHeight}`,
25+
}
26+
}, [originalSvgHeight, originalSvgWidth, containerWidth])
27+
}

packages/expo/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ export * from './utils/parseConstants'
33
export * from './utils/parseGradient'
44
export * from './utils/conditionalMarkup'
55

6+
// Hooks
7+
export * from './hooks/useSvgDimensions'
8+
69
// Components
710
export * from './components/Gradient'

0 commit comments

Comments
 (0)