tomat / react-auto-scale Goto Github PK
View Code? Open in Web Editor NEWWrap a single child with this component to make it auto-scale (transform) to fit its parent (or a maximum size specified through props).
License: MIT License
Wrap a single child with this component to make it auto-scale (transform) to fit its parent (or a maximum size specified through props).
License: MIT License
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>
);
};
Is it possible to have it autoscale so the content is always contained within the container/Viewport?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.