Skip to content

Feat/to type script #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/*

node_modules
lib
24 changes: 10 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import useDimensions from './lib/useDimensions'
import useAppState from './lib/useAppState'
import useCameraRoll from './lib/useCameraRoll'
import useClipboard from './lib/useClipboard'
import useAccessibilityInfo from './lib/useAccessibilityInfo'
import useGeolocation from './lib/useGeolocation'
import useNetInfo from './lib/useNetInfo'
import useKeyboard from './lib/useKeyboard'
import useInteractionManager from './lib/useInteractionManager'
import useDeviceOrientation from './lib/useDeviceOrientation'
import useDimensions from "./lib/useDimensions";
import useAppState from "./lib/useAppState";
import useCameraRoll from "./lib/useCameraRoll";
import useClipboard from "./lib/useClipboard";
import useAccessibilityInfo from "./lib/useAccessibilityInfo";
import useKeyboard from "./lib/useKeyboard";
import useInteractionManager from "./lib/useInteractionManager";
import useDeviceOrientation from "./lib/useDeviceOrientation";

export {
useDimensions,
useAppState,
useCameraRoll,
useClipboard,
useAccessibilityInfo,
useGeolocation,
useNetInfo,
useKeyboard,
useInteractionManager,
useDeviceOrientation,
}
useDeviceOrientation
};
50 changes: 0 additions & 50 deletions lib/useGeolocation.js

This file was deleted.

37 changes: 0 additions & 37 deletions lib/useKeyboard.js

This file was deleted.

30 changes: 0 additions & 30 deletions lib/useNetInfo.js

This file was deleted.

17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
"name": "react-native-hooks",
"version": "0.0.6",
"description": "",
"main": "index.js",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "tsc"
},
"author": "Nader Dabit",
"license": "ISC",
"dependencies": {
"react": "^16.7.0-alpha.0"
}
},
"devDependencies": {
"@types/react": "^16.9.10",
"@types/react-native": "^0.60.22",
"typescript": "^3.6.4"
},
"files": [
"index.js",
"lib"
]
}
15 changes: 8 additions & 7 deletions lib/useAccessibilityInfo.js → src/useAccessibilityInfo.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import React, { useEffect, useState } from 'react'
import { AccessibilityInfo } from 'react-native'
import { useEffect, useState } from 'react'
import { AccessibilityInfo, AccessibilityEvent } from 'react-native'


export default () => {
const [screenReaderEnabled, updateScreenReaderInfo] = useState(null)
const [screenReaderEnabled, updateScreenReaderInfo] = useState<AccessibilityEvent|null>(null)


useEffect(() => {
AccessibilityInfo.fetch().then((isEnabled) => {
AccessibilityInfo.fetch().then((isEnabled: AccessibilityEvent) => {
updateScreenReaderInfo(isEnabled)
})
}, [])

function onChange(isEnabled) {
function onChange(isEnabled: AccessibilityEvent) {
updateScreenReaderInfo(isEnabled)
}

useEffect(() => {
AccessibilityInfo.addEventListener('change', onChange)

return () => AccessibilityInfo.removeEventListener('change', onChange)
}, [])

return screenReaderEnabled
}
}
7 changes: 4 additions & 3 deletions lib/useAppState.js → src/useAppState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { useEffect, useState } from 'react'
import { AppState } from 'react-native'
import { AppState, AppStateStatus } from 'react-native'


const currentState = AppState.currentState

export default () => {
const [appState, setAppState] = useState(currentState)

function onChange (newState) {
function onChange(newState: AppStateStatus) {
setAppState(newState)
}

Expand All @@ -19,4 +20,4 @@ export default () => {
})

return appState
}
}
13 changes: 7 additions & 6 deletions lib/useCameraRoll.js → src/useCameraRoll.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { useState } from 'react'
import {
CameraRoll
CameraRoll, GetPhotosParamType, GetPhotosReturnType
} from 'react-native'

const initialState = {

const initialState: GetPhotosReturnType = {
edges: [],
page_info: {
end_cursor: '',
has_next_page: null,
has_next_page: false,
start_cursor: ''
}
}

const defaultConfig = {
const defaultConfig: GetPhotosParamType = {
first: 20,
groupTypes: 'All'
}
Expand All @@ -29,7 +30,7 @@ export default function() {
}
}

async function saveToCameraRoll(tag, type) {
async function saveToCameraRoll(tag:string, type:'photo'|'video'|undefined) {
try {
await CameraRoll.saveToCameraRoll(tag, type)
} catch (err) {
Expand All @@ -38,4 +39,4 @@ export default function() {
}

return [photos, getPhotos, saveToCameraRoll]
}
}
11 changes: 7 additions & 4 deletions lib/useClipboard.js → src/useClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React, { useEffect, useState } from 'react'
import { Clipboard } from 'react-native'


export default () => {
const [data, updateClipboardData] = useState('')

useEffect(async () => {
const content = await Clipboard.getString()
updateClipboardData(content)
useEffect(() => {
(async () => {
const content = await Clipboard.getString()
updateClipboardData(content)
})()
}, [])

function setString(content) {
function setString(content: string) {
Clipboard.setString(content)
updateClipboardData(content)
}
Expand Down
11 changes: 6 additions & 5 deletions lib/useDeviceOrientation.js → src/useDeviceOrientation.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { useEffect, useState } from 'react';
import { Dimensions } from 'react-native';
import { Dimensions, ScaledSize } from 'react-native';


const screen = Dimensions.get('screen');

export default () => {
const isOrientationPortrait = ({ width, height }: { width: number, height: number }) => height >= width;
const isOrientationLandscape = ({ width, height }: { width: number, height: number }) => width >= height;

const [orientation, setOrientation] = useState({
portrait: isOrientationPortrait(screen),
landscape: isOrientationLandscape(screen)
});

isOrientationPortrait = ({ width, height }) => height >= width;
isOrientationLandscape = ({ width, height }) => width >= height;

onChange = ({ screen }) => {
const onChange = ({ screen }: { screen: ScaledSize }) => {
setOrientation({
portrait: isOrientationPortrait(screen),
landscape: isOrientationLandscape(screen)
Expand Down
7 changes: 4 additions & 3 deletions lib/useDimensions.js → src/useDimensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useState } from 'react'
import { Dimensions } from 'react-native'
import { Dimensions, ScaledSize } from 'react-native'


const window = Dimensions.get('window')
const screen = Dimensions.get('screen')
Expand All @@ -9,7 +10,7 @@ export default () => {
window, screen
})

onChange = ({ window, screen }) => {
const onChange = ({ window, screen }: { window: ScaledSize, screen: ScaledSize }) => {
setDimensions({ window, screen })
}

Expand All @@ -20,4 +21,4 @@ export default () => {
}, [])

return dimensions
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useState } from 'react';
import { InteractionManager } from 'react-native';


export default () => {
const [complete, updateInteractionStatus] = useState(false);

Expand Down
Loading