react-use#useMedia TypeScript Examples

The following examples show how to use react-use#useMedia. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: use-hooks.tsx    From erda-ui with GNU Affero General Public License v3.0 6 votes vote down vote up
useMediaRange = (main = false, ignoreWidth = defaultIgnoreWidth) => {
  const extraWidth = main ? ignoreWidth : 0;

  const largeThanXs = useMedia(`(min-width: ${ScreenSize.xs + extraWidth}px)`);
  const largeThanSm = useMedia(`(min-width: ${ScreenSize.sm + extraWidth}px)`);
  const largeThanMd = useMedia(`(min-width: ${ScreenSize.md + extraWidth}px)`);
  const largeThanLg = useMedia(`(min-width: ${ScreenSize.lg + extraWidth}px)`);
  const largeThanXl = useMedia(`(min-width: ${ScreenSize.xl + extraWidth}px)`);
  return [largeThanXs, largeThanSm, largeThanMd, largeThanLg, largeThanXl];
}
Example #2
Source File: use-hooks.tsx    From erda-ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useMediaSize = () => {
  const largeThan600 = useMedia(`(min-width: ${600 + defaultIgnoreWidth}px)`);
  const largeThan1024 = useMedia(`(min-width: ${1024 + defaultIgnoreWidth}px)`);
  const largeThan1440 = useMedia(`(min-width: ${1440 + defaultIgnoreWidth}px)`);
  return [largeThan600, largeThan1024, largeThan1440];
}
Example #3
Source File: use-hooks.tsx    From erda-ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useMediaGt = (num: number, main = false, ignoreWidth = defaultIgnoreWidth) =>
  useMedia(`(min-width: ${num + (main ? ignoreWidth : 0)}px)`)
Example #4
Source File: use-hooks.tsx    From erda-ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useMediaLt = (num: number, main = false, ignoreWidth = defaultIgnoreWidth) =>
  useMedia(`(max-width: ${num + (main ? ignoreWidth : 0)}px)`)
Example #5
Source File: app.tsx    From flood with GNU General Public License v3.0 5 votes vote down vote up
FloodApp: FC = observer(() => {
  useEffect(() => {
    UIStore.registerDependency([
      {
        id: 'notifications',
        message: {id: 'dependency.loading.notifications'},
      },
    ]);

    UIStore.registerDependency([
      {
        id: 'torrent-taxonomy',
        message: {id: 'dependency.loading.torrent.taxonomy'},
      },
    ]);

    UIStore.registerDependency([
      {
        id: 'transfer-data',
        message: {id: 'dependency.loading.transfer.rate.details'},
      },
      {
        id: 'transfer-history',
        message: {id: 'dependency.loading.transfer.history'},
      },
    ]);

    UIStore.registerDependency([
      {
        id: 'torrent-list',
        message: {id: 'dependency.loading.torrent.list'},
      },
    ]);
  }, []);

  const isSystemPreferDark = useMedia('(prefers-color-scheme: dark)');
  useEffect(() => {
    ConfigStore.setSystemPreferDark(isSystemPreferDark);
  }, [isSystemPreferDark]);

  // max-width here must sync with CSS
  const isSmallScreen = useMedia('(max-width: 720px)');
  useEffect(() => {
    ConfigStore.setSmallScreen(isSmallScreen);
  }, [isSmallScreen]);

  return (
    <Suspense fallback={<LoadingOverlay />}>
      <AsyncIntlProvider>
        <BrowserRouter basename={stringUtil.withoutTrailingSlash(ConfigStore.baseURI)}>
          <AppWrapper className={ConfigStore.isPreferDark ? 'dark' : undefined}>
            <Routes>
              <Route path="/login" element={<Login />} />
              <Route path="/overview" element={<Overview />} />
              <Route path="/register" element={<Register />} />
            </Routes>
          </AppWrapper>
        </BrowserRouter>
      </AsyncIntlProvider>
    </Suspense>
  );
})
Example #6
Source File: TimePickerContent.tsx    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
TimePickerContent: React.FC<Props> = props => {
  const theme = useTheme();
  const isFullscreen = useMedia(`(min-width: ${theme.breakpoints.lg})`);

  return <TimePickerContentWithScreenSize {...props} isFullscreen={isFullscreen} />;
}
Example #7
Source File: Navbar.tsx    From XFlow with MIT License 4 votes vote down vote up
Navbar: FC<INavbarProps> = ({ onMobileMenuClick, navPrefix, location }) => {
  const {
    base,
    config: { mode, title, logo },
    nav: navItems,
  } = useContext(context);

  const isWide = useMedia('(min-width: 767.99px)', true);

  const [productMenuVisible, setProductMenuVisible] = React.useState(false);
  let productMenuHovering = false;
  const onProductMouseEnter = (e: React.MouseEvent) => {
    productMenuHovering = true;
    e.persist();
    setTimeout(() => {
      // eslint-disable-next-line no-undef
      if (e.target instanceof Element && e.target.matches(':hover')) {
        setProductMenuVisible(true);
      }
    }, 200);
  };
  const onProductMouseLeave = (e: React.MouseEvent) => {
    e.persist();
    productMenuHovering = false;
    setTimeout(() => {
      if (productMenuHovering) {
        return;
      }
      setProductMenuVisible(false);
    }, 200);
  };
  const onToggleProductMenuVisible = () => {
    setProductMenuVisible(!productMenuVisible);
  };

  const productItemProps = isWide
    ? {
        onMouseEnter: onProductMouseEnter,
        onMouseLeave: onProductMouseLeave,
      }
    : {
        onClick: onToggleProductMenuVisible,
      };

  return (
    <div className="__dumi-default-navbar" data-mode={mode}>
      {/* menu toogle button (only for mobile) */}
      <button className="__dumi-default-navbar-toggle" onClick={onMobileMenuClick} />
      {/* logo & title */}
      <Link
        className="__dumi-default-navbar-logo"
        style={{
          background: 'none',
          height: '28px',
          lineHeight: '28px',
          paddingLeft: '0px',
        }}
        to={base}
        data-plaintext={logo === false || undefined}
      >
        <Logo style={{ height: '28px', lineHeight: '28px' }} />
        <span
          style={{
            fontSize: '16px',
            width: '1px',
            height: '24px',
            backgroundColor: '#ccc',
            display: ' inline-block',
            margin: '0 20px',
          }}
        />
        <span style={{ fontSize: '16px', color: '#0d1a26', display: 'inline-block', verticalAlign: 'top' }}>
          {title}
        </span>
      </Link>

      <nav>
        {navPrefix}
        {/* nav */}
        {navItems.map(nav => {
          const child = Boolean(nav.children?.length) && (
            <ul>
              {nav.children.map(item => (
                <li key={item.path}>
                  <NavLink to={item.path}>{item.title}</NavLink>
                </li>
              ))}
            </ul>
          );

          return (
            <span key={nav.title || nav.path}>
              {nav.path ? (
                <NavLink to={nav.path} key={nav.path}>
                  {nav.title}
                </NavLink>
              ) : (
                nav.title
              )}
              {child}
            </span>
          );
        })}
        <span {...productItemProps}>
          ζ‰€ζœ‰δΊ§ε“
          <img
            src="https://gw.alipayobjects.com/zos/antfincdn/FLrTNDvlna/antv.png"
            alt="antv logo arrow"
            className={`arrow ${productMenuVisible && 'open'}`}
          />
          <div
            style={{
              position: 'fixed',
              top: ' 0px',
              left: '0px',
              width: ' 100%',
              right: '0px',
            }}
          >
            <Products show={productMenuVisible} rootDomain="ant.vison" language="zh" />
          </div>
        </span>
        <LocaleSelect location={location} />
      </nav>
    </div>
  );
}