Code Monkey home page Code Monkey logo

react-auto-scale's People

Contributors

tomat avatar valstu 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

Watchers

 avatar  avatar  avatar

react-auto-scale's Issues

Autoscale contained

Is it possible to have it autoscale so the content is always contained within the container/Viewport?

For any Typescript users.

There are no @types available for this package so you might be better off copy&pasting this. Please note: You will need to install element-resize-event and @types/element-resize-event packages.

import React, { useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import elementResizeEvent, { unbind } from 'element-resize-event';

const RelativeRoot = styled.div`
  position: relative;
`;

const Wrapper = styled.div`
  position: absolute;
  width: 100%;
`;

const Container = styled.div`
  max-width: 100%;
  overflow: hidden;
`;

const Content = styled.div`
  transform-origin: 0 0 0;
`;

type Size = {
  width: number;
  height: number;
};

export type AutoScaleProps = {
  maxHeight?: number;
  maxWidth?: number;
  maxScale?: number;
};

export const AutoScale: React.FC<AutoScaleProps> = ({ maxWidth, maxHeight, maxScale, children }) => {
  const wrapperRef = useRef<HTMLDivElement>(null);
  const contentRef = useRef<HTMLDivElement>(null);

  const [contentSize, setContentSize] = useState<Size>({ width: 0, height: 0 });
  const [scale, setScale] = useState<number>(1);

  const updateState = () => {
    const actualContent = contentRef.current!.children[0] as HTMLElement;
    const _wrapperSize: Size = { width: wrapperRef.current!.offsetWidth, height: wrapperRef.current!.offsetHeight };
    const _contentSize: Size = { width: actualContent.offsetWidth, height: actualContent.offsetHeight };

    let _scale = (_wrapperSize.width / _contentSize.width);

    if (maxHeight) {
      _scale = Math.min(_scale, (maxHeight / _contentSize.height));
    }
    if (maxWidth) {
      _scale = Math.min(_scale, (maxWidth / _contentSize.width));
    }
    if (maxScale) {
      _scale = Math.min(_scale, maxScale);
    }

    setContentSize(_contentSize);
    setScale(_scale);
  };

  useEffect(() => {
    const actualContent = contentRef.current!.children[0] as HTMLElement;
    updateState();
    elementResizeEvent(actualContent, updateState);
    elementResizeEvent(wrapperRef.current!, updateState);
    return () => {
      unbind(actualContent);
      unbind(wrapperRef.current!);
    };
  }, []);

  const containerStyles = {
    width: scale * contentSize.width,
    height: scale * contentSize.height,
  };

  const contentStyles = {
    transform: `scale(${scale})`,
  };

  return (
    <RelativeRoot>
      <Wrapper ref={wrapperRef}>
        <Container style={containerStyles}>
          <Content ref={contentRef} style={contentStyles}>
            {children}
          </Content>
        </Container>
      </Wrapper>
    </RelativeRoot>
  );
};

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.