{"id":13475858,"url":"https://github.com/EvanBacon/react-native-web-hooks","last_synced_at":"2025-03-27T01:30:27.546Z","repository":{"id":45437833,"uuid":"193578611","full_name":"EvanBacon/react-native-web-hooks","owner":"EvanBacon","description":"Hooks for React Native web and Expo","archived":false,"fork":false,"pushed_at":"2021-12-13T21:43:18.000Z","size":285,"stargazers_count":198,"open_issues_count":9,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-18T13:17:54.742Z","etag":null,"topics":["hooks","react","react-native","react-native-web"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EvanBacon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-24T20:52:12.000Z","updated_at":"2024-12-12T16:08:45.000Z","dependencies_parsed_at":"2022-09-26T17:41:05.755Z","dependency_job_id":null,"html_url":"https://github.com/EvanBacon/react-native-web-hooks","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvanBacon%2Freact-native-web-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvanBacon%2Freact-native-web-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvanBacon%2Freact-native-web-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvanBacon%2Freact-native-web-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EvanBacon","download_url":"https://codeload.github.com/EvanBacon/react-native-web-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245764577,"owners_count":20668445,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","react","react-native","react-native-web"],"created_at":"2024-07-31T16:01:24.161Z","updated_at":"2025-03-27T01:30:27.201Z","avatar_url":"https://github.com/EvanBacon.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# react-native-web-hooks\n\nHooks for implementing complex functionality in React Native for web and Expo.\n\nA closer look at [how the hooks work here](https://gist.github.com/EvanBacon/8739dc52a4dbb72e869f19b1e5cdda6c).\n\n## Installation\n\n```bash\nyarn add react-native-web-hooks\n\nor\n\nnpm install --save react-native-web-hooks\n```\n\n### Usage - Hooks\n\nImport the library into your JavaScript file:\n\n```js\nimport {\n  useDimensions,\n  useActive,\n  useFocus,\n  useHover,\n  useREM,\n  useScaledSize,\n} from 'react-native-web-hooks';\n```\n\n#### Get REM size\n\nUse these in place of rem font sizes like: `font-size: 1.3rem`.\n\n\u003e Note: this isn't a hook anymore and will be moved out in the future.\n\n```jsx\nconst fontSize = useREM(1.3);\n\nreturn \u003cText style={{ fontSize }} /\u003e;\n```\n\n#### Get scaled font size\n\nThese change based on the width of the screen.\n\n```jsx\nconst fontSize = useScaledSize(1.5);\n\nreturn \u003cText style={{ fontSize }} /\u003e;\n```\n\n#### Get dimensions\n\nNote that `fontScale` is hard-coded to `1` on the `react-native-web` side and shouldn't be used to calculate dynamic font sizes.\n\n```jsx\nconst {\n  window: { width, height, fontScale, scale },\n  screen,\n} = useDimensions();\n```\n\n#### Measure a view\n\nIt's best to style a view based on that own view's size and not the window size. To make this easier you can use the `useLayout` hook!\n\n\u003e 🚨 Using `onLayout` may require you to install `resize-observer-polyfill`. Learn more in [the official Expo docs](https://docs.expo.io/versions/latest/guides/customizing-webpack/#resizeobserver)\n\n```jsx\nconst {\n  onLayout,\n  width,\n  height,\n  x,\n  y\n} = useLayout();\n\nreturn \u003cView onLayout={onLayout} /\u003e\n```\n\n#### Create pseudo class styles\n\nThese will be replaced by **React Flare** when it's released.\n\n```jsx\nimport { useRef } from 'react';\nimport { StyleSheet, Linking, Text, Platform } from 'react-native';\n\nimport { useHover, useFocus, useActive } from 'react-native-web-hooks';\n\nfunction Link({ children, href = '#' }) {\n  const ref = useRef(null);\n\n  const isHovered = useHover(ref);\n  const isFocused = useFocus(ref);\n  const isActive = useActive(ref);\n\n  return (\n    \u003cText\n      accessibilityRole=\"link\"\n      href={href}\n      draggable={false}\n      onPress={() =\u003e Linking.openURL(href)}\n      tabIndex={0}\n      ref={ref}\n      style={[\n        styles.text,\n        isHovered \u0026\u0026 styles.hover,\n        isFocused \u0026\u0026 styles.focused,\n        isActive \u0026\u0026 styles.active,\n      ]}\u003e\n      {children}\n    \u003c/Text\u003e\n  );\n}\n\nconst styles = StyleSheet.create({\n  text: {\n    ...Platform.select({\n      web: {\n        cursor: 'pointer',\n        outlineStyle: 'none',\n        borderBottomWidth: 1,\n        borderBottomColor: 'transparent',\n        transitionDuration: '200ms',\n      },\n      default: {},\n    }),\n  },\n  active: {\n    color: 'blue',\n    borderBottomColor: 'blue',\n    opacity: 1.0,\n  },\n  hover: {\n    opacity: 0.6,\n  },\n  focused: {\n    borderBottomColor: 'black',\n  },\n});\n```\n\n### Usage - Render Props\n\nImport the library into your JavaScript file:\n\n```js\nimport { Hoverable, Resizable } from 'react-native-web-hooks';\n```\n\nYou can wrap a function or a component.\n\n```tsx\nimport React, { Component } from 'react';\nimport { Text, TouchableOpacity, View } from 'react-native';\nimport { Hoverable } from 'react-native-web-hooks';\n\nconst createLogger = (...msg) =\u003e () =\u003e {\n  console.log(...msg);\n};\n\nclass App extends Component {\n  render() {\n    return (\n      \u003cView\u003e\n        \u003cHoverable onHoverIn={createLogger('start hover')} onHoverOut={createLogger('end hover')}\u003e\n          {isHovered =\u003e (\n            \u003cTouchableOpacity accessible style={{ backgroundColor: isHovered ? '#333' : '#fff' }}\u003e\n              \u003cText\u003eWelcome to React\u003c/Text\u003e}\n            \u003c/TouchableOpacity\u003e\n          )}\n        \u003c/Hoverable\u003e\n      \u003c/View\u003e\n    );\n  }\n}\n```\n\nObserve window resize events.\n\n```tsx\nreturn (\n  \u003cResizable\u003e\n    {layout =\u003e \u003cView style={{ width: layout.width / 2, height: layout.width / 2 }} /\u003e}\n  \u003c/Resizable\u003e\n);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEvanBacon%2Freact-native-web-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEvanBacon%2Freact-native-web-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEvanBacon%2Freact-native-web-hooks/lists"}