All Projects → EvanBacon → React Native Web Hooks

EvanBacon / React Native Web Hooks

Licence: mit
Hooks for React Native web and Expo

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Native Web Hooks

Tipple
A lightweight dependency-free library for fetching data over REST with React.
Stars: ✭ 133 (-15.29%)
Mutual labels:  hooks
Precommit
pre-commit hooks for R projects
Stars: ✭ 141 (-10.19%)
Mutual labels:  hooks
React Use Clipboard
React hook that provides copy to clipboard functionality.
Stars: ✭ 149 (-5.1%)
Mutual labels:  hooks
React Atom
A simple way manage state in React, inspired by Clojure(Script) and reagent.cljs
Stars: ✭ 133 (-15.29%)
Mutual labels:  hooks
Expo Dark Mode Switch
A beautiful React dark mode switch component for iOS, Android, and Web
Stars: ✭ 137 (-12.74%)
Mutual labels:  react-native-web
Tabs
React tabs with hooks
Stars: ✭ 147 (-6.37%)
Mutual labels:  hooks
Window Size
React hook for subscribing to window size
Stars: ✭ 130 (-17.2%)
Mutual labels:  hooks
Use Url State
Lift a React component's state into the url
Stars: ✭ 154 (-1.91%)
Mutual labels:  hooks
React Circular Input
React components for easily composing a circular range input
Stars: ✭ 141 (-10.19%)
Mutual labels:  hooks
Redhooks
Predictable state container for React apps written using Hooks
Stars: ✭ 149 (-5.1%)
Mutual labels:  hooks
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (-15.29%)
Mutual labels:  hooks
Use Less
React hooks that help you do what you already did, with more indirection
Stars: ✭ 135 (-14.01%)
Mutual labels:  hooks
React Universal Hooks
🎉 React Universal Hooks : just use****** everywhere (Functional or Class Component). Support React DevTools!
Stars: ✭ 148 (-5.73%)
Mutual labels:  hooks
React Use Scrollspy
Flexible React Hook to automatically update navigation based on scroll position
Stars: ✭ 133 (-15.29%)
Mutual labels:  hooks
React Infinite Scroll Hook
A simple hook to create infinite scroll list components
Stars: ✭ 151 (-3.82%)
Mutual labels:  hooks
Mern Authentication
MERN stack authentication boilerplate: password reset, email verification, server sessions, redux, hooks and docker for dev and prod.
Stars: ✭ 129 (-17.83%)
Mutual labels:  hooks
React Storage Hooks
React hooks for persistent state
Stars: ✭ 146 (-7.01%)
Mutual labels:  hooks
React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+1612.74%)
Mutual labels:  hooks
React Model
The next generation state management library for React
Stars: ✭ 153 (-2.55%)
Mutual labels:  hooks
Distormx
The ultimate hooking library
Stars: ✭ 146 (-7.01%)
Mutual labels:  hooks

react-native-web-hooks

Hooks for implementing complex functionality in React Native for web and Expo.

A closer look at how the hooks work here.

Installation

yarn add react-native-web-hooks

or

npm install --save react-native-web-hooks

Usage - Hooks

Import the library into your JavaScript file:

import {
  useDimensions,
  useActive,
  useFocus,
  useHover,
  useREM,
  useScaledSize,
} from 'react-native-web-hooks';

Get REM size

Use these in place of rem font sizes like: font-size: 1.3rem.

Note: this isn't a hook anymore and will be moved out in the future.

const fontSize = useREM(1.3);

return <Text style={{ fontSize }} />;

Get scaled font size

These change based on the width of the screen.

const fontSize = useScaledSize(1.5);

return <Text style={{ fontSize }} />;

Get dimensions

Note that fontScale is hard-coded to 1 on the react-native-web side and shouldn't be used to calculate dynamic font sizes.

const {
  window: { width, height, fontScale, scale },
  screen,
} = useDimensions();

Measure a view

It'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!

🚨 Using onLayout may require you to install resize-observer-polyfill. Learn more in the official Expo docs

const {
  onLayout,
  width,
  height,
  x,
  y
} = useLayout();

return <View onLayout={onLayout} />

Create pseudo class styles

These will be replaced by React Flare when it's released.

import { useRef } from 'react';
import { StyleSheet, Linking, Text, Platform } from 'react-native';

import { useHover, useFocus, useActive } from 'react-native-web-hooks';

function Link({ children, href = '#' }) {
  const ref = useRef(null);

  const isHovered = useHover(ref);
  const isFocused = useFocus(ref);
  const isActive = useActive(ref);

  return (
    <Text
      accessibilityRole="link"
      href={href}
      draggable={false}
      onPress={() => Linking.openURL(href)}
      tabIndex={0}
      ref={ref}
      style={[
        styles.text,
        isHovered && styles.hover,
        isFocused && styles.focused,
        isActive && styles.active,
      ]}>
      {children}
    </Text>
  );
}

const styles = StyleSheet.create({
  text: {
    ...Platform.select({
      web: {
        cursor: 'pointer',
        outlineStyle: 'none',
        borderBottomWidth: 1,
        borderBottomColor: 'transparent',
        transitionDuration: '200ms',
      },
      default: {},
    }),
  },
  active: {
    color: 'blue',
    borderBottomColor: 'blue',
    opacity: 1.0,
  },
  hover: {
    opacity: 0.6,
  },
  focused: {
    borderBottomColor: 'black',
  },
});

Usage - Render Props

Import the library into your JavaScript file:

import { Hoverable, Resizable } from 'react-native-web-hooks';

You can wrap a function or a component.

import React, { Component } from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import { Hoverable } from 'react-native-web-hooks';

const createLogger = (...msg) => () => {
  console.log(...msg);
};

class App extends Component {
  render() {
    return (
      <View>
        <Hoverable onHoverIn={createLogger('start hover')} onHoverOut={createLogger('end hover')}>
          {isHovered => (
            <TouchableOpacity accessible style={{ backgroundColor: isHovered ? '#333' : '#fff' }}>
              <Text>Welcome to React</Text>}
            </TouchableOpacity>
          )}
        </Hoverable>
      </View>
    );
  }
}

Observe window resize events.

return (
  <Resizable>
    {layout => <View style={{ width: layout.width / 2, height: layout.width / 2 }} />}
  </Resizable>
);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].