Giter VIP home page Giter VIP logo

react-native-web-hooks's Introduction

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>
);

react-native-web-hooks's People

Contributors

dependabot[bot] avatar evanbacon avatar jaulz avatar simek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-native-web-hooks's Issues

Weekly Digest (26 January, 2020 - 2 February, 2020)

Here's the Weekly Digest for EvanBacon/react-native-web-hooks:


ISSUES

Last week 1 issue was created.
It is still open.

OPEN ISSUES

๐Ÿ’š #11 Hoverable does not work with react-native-web 12.0, by jsp3536


PULL REQUESTS

Last week, no pull requests were created, updated or merged.


COMMITS

Last week there were no commits.


CONTRIBUTORS

Last week there were no contributors.


STARGAZERS

Last week there were 4 stagazers.
โญ majidux
โญ LeoLeBras
โญ artyorsh
โญ comontes
You all are the stars! ๐ŸŒŸ


RELEASES

Last week there were no releases.


That's all for last week, please ๐Ÿ‘€ Watch and โญ Star the repository EvanBacon/react-native-web-hooks to receive next weekly updates. ๐Ÿ˜ƒ

You can also view all Weekly Digests by clicking here.

Your Weekly Digest bot. ๐Ÿ“†

Any way to get current zoom?

With Ctrl+Scroll or + or -, we can change the page zoom, or pinching on mobile.

Is there someway to get the current user zoom?

Types issue for useREM

Hi @EvanBacon

I wonder how you solve this:

image

fontSize must be a number according to RN typedefs, yet useREM might return a string on web and is string | number

useHover and opacity

As far as I can tell, the default use case of useHover is making the hovered item semi-transparent.

Is there a way to override this?

I am creating a drowndown menu component, and I do not want any transparency when a user hovers over a dropdown menu item

Weekly Digest (23 January, 2020 - 30 January, 2020)

Here's the Weekly Digest for EvanBacon/react-native-web-hooks:


ISSUES

Last week 1 issue was created.
It is still open.

OPEN ISSUES

๐Ÿ’š #9 Types issue for useREM, by slorber


PULL REQUESTS

Last week, no pull requests were created, updated or merged.


COMMITS

Last week there were no commits.


CONTRIBUTORS

Last week there were no contributors.


STARGAZERS

Last week there were 4 stagazers.
โญ byCedric
โญ majidux
โญ LeoLeBras
โญ artyorsh
You all are the stars! ๐ŸŒŸ


RELEASES

Last week there were no releases.


That's all for last week, please ๐Ÿ‘€ Watch and โญ Star the repository EvanBacon/react-native-web-hooks to receive next weekly updates. ๐Ÿ˜ƒ

You can also view all Weekly Digests by clicking here.

Your Weekly Digest bot. ๐Ÿ“†

getNode is being phased out

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

This resolved a warning about getNode being deprecated in the future.

Screenshot:
Screenshot 2023-03-29 at 10 07 57 AM

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-web-hooks/build/createPseudoHook.js b/node_modules/react-native-web-hooks/build/createPseudoHook.js
index 2c544fb..002cda5 100644
--- a/node_modules/react-native-web-hooks/build/createPseudoHook.js
+++ b/node_modules/react-native-web-hooks/build/createPseudoHook.js
@@ -11,7 +11,7 @@ export default function createPseudoHook({ events }) {
         const [isActive, setActive] = React.useState(false);
         React.useEffect(() => {
             const [eventIn, eventOut] = events;
-            const node = getNode(ref);
+            const node = ref.current;
             if (!node) {
                 return;
             }

This issue body was partially generated by patch-package.

Types?

Thanks for the great library which lets me replace my custom hooks now. Do you have any current plans for migrating to Typescript or adding types?

SSR support

Still trying to build https://github.com/expo/examples/tree/master/with-gatsby in production mode, with real SSR.

It's almost there, but this library does not support SSR:

image

Not sure exactly how this can be fixed. Can we compute the output of useREM on SSR?

getComputedStyle(document.documentElement).fontSize can't work on the frontend. One possibility could be to add a default fallback value for SSR, and allow to override that value through context on the server? Like <RNWHProvider value={{documentFontSize: 16}}/>?

Another option would be to provide a default fallback value as 2nd arg, like useREM(1.3, 16);

BTW, is useREM really a hook? it does not use any primitive React hook so it's just a function call (that currently only works in the browser)

Guidance on testing

@EvanBacon for lack of a better place. I am currently using this in leading an effort to implement a component library across react native Web, iOS, Android that has heavy testing requirements. How would you go about testing hover states of buttons. Would you use react testing library with react native web, or would there be a way to use react native testing library?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.