npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-scaled-layout

v1.1.7

Published

Flexible, Scalable layout dimensions, font sizes for React Native

Downloads

20

Readme

react-native-scaled-layout

npm

Sorry to Api changes in 1.1.0 😢

Detail Article's Link 🔗

Flexible, Scalable layout dimensions, font sizes for React Native

react-native-scaled-layout is using monkey-patch feature in javascript(typescript) and augmentation syntax in typescript.

Contents 🏆

Install 💠

npm i react-native-scaled-layout

or

yarn add react-native-scaled-layout

Usage 📌

0. Configure your environment or wanted behavior

❕ If react-native-scaled-layout is not imported for side-effect, then TypeError will be invoked.

index.js

import { initScaledSettings } from 'react-native-scaled-layout';
...
initScaledSettings(375, { min: 0.5, max: 1.5 }, { min: 0.75, max: 1.35 }, 14);

| Params | Type | Default | Required | | ------------------- |---------|---------|----------| | designSpecWidth | number | 375 | false | | dimenScaleRange | { min: number; max: number } | [0.5, 1.5] | false | | fontScaleRange | { min: number; max: number } | [0.75, 1.3] | false | | defaultFontSize | number | 14 | false |

1. Number type Augmentation(Extension)

// calculated with width length of design spec
// clamped with dimenScaleRange min, max value
(36).scaled() /* or */ (36).d() 

// calculated with width length of design spec
// clamped with fontScaleRange min, max value
(24).fontScaled() /* or */ (24).f()

Example in ViewStyle

style={{
    width: (100).d(),
    height: (210).d() + safeAreaBottom,
    borderRadius: (16).d(),
    justifyContent: 'center',
    paddingBottom: safeAreaBottom + (24).d(),
}}

2. ScaledText Component

// automatically adjusted with (14).fontScaled()
<ScaledText style={{fontSize: 14}}>My Text</ScaledText> 

// ignore calculated font scale
// fixed with 28(14 * 2)
<ScaledText style={{fontSize: 14}} customFontScale={isTablet ? 2 : undefined}>My Text</ScaledText>

react-native-scaled-layout is also compatible with Styled Component

export const BoldText = styled(ScaledText)`
  font-family: ${fonts.NotoSansKRBold};
`;
...
const TutorialText = styled(BoldText)`
  left: ${(20).d()}px;
  right: ${(20).d()}px;
  position: absolute;
  font-size: 24px; // automatically adjust font size with (24).fontScaled()
  color: ${({ theme }): string => theme.white};
`;

Calculation 📐

The following is the implementation of initScaledSettings

/**
 * Set initial configuration for scaled layout behavior. If your height of design guideline spec is less than width, invert 1st, 2nd params.
 * @param designSpecWidth your design width viewport width(zeplin, pigma etc...). If your design viewport is 375 x 1000 then 375 is a right value.
 * @param dimenScaleRange dimension scale factor minimum & maximum range. default is [0.5, 1.5]..
 * @param fontScaleRange font scale factor minimum & maximum range. default is [0.75, 1.3].
 * @param defaultFontsize default `<Text>` fontSize. default is 12.
 *
 * @example
 * ```ts
 * initScaledSettings(375, 812, {min: 0.5, max: 1.5}, {min: 0.75, max: 1.3}, 12);
 * ```
 */
export function initScaledSettings(
  designSpecWidth = 375,
  dimenScaleRange: { min: number; max: number } = { min: 0.5, max: 1.5 },
  fontScaleRange: { min: number; max: number } = { min: 0.75, max: 1.3 },
  defaultFontsize = 12,
): void {
  dimenRatio = minLength / designSpecWidth;

  dimenScale = clamp(dimenRatio, dimenScaleRange.min, dimenScaleRange.max);

  fontScale =
    dimenScale >= 1 ? Math.min(dimenScale, fontScaleRange.max) : Math.max(dimenScale * dimenScale, fontScaleRange.min);
  _FONT_SCALE_ = fontScale;
  _defaultFontSize = defaultFontsize;

  /* eslint-disable no-extend-native */
  Number.prototype.scaled = function scaled(): number {
    return Math.round((this as number) * dimenScale);
  };
  Number.prototype.fontScaled = function fontScaled(): number {
    return Math.round((this as number) * fontScale);
  };
  Number.prototype.d = function d(): number {
    return (this as number).scaled();
  };
  Number.prototype.f = function f(): number {
    return (this as number).fontScaled();
  };
  /* eslint-enable no-extend-native */
}
initScaledSettings();

Trouble Shooting❗️

1. TypeError: 40.d is not a function

Please put import 'react-native-scaled-layout' to top of index.js or top of file which in setupFiles list of jest.config.js

Todo ✅

  • Create ScaledView, ScaledTextInput, ScaledTouchableXXX like ScaledText

Change Logs 🔧

  • 1.1.6
    • The values are calculated with ceil() instead of round().
  • 1.1.4
    • minimumFontSize prop is added in ScaledText
  • 1.1.2
    • dimenWidthScaled(), dimenHeightScaled(), w(), h() are removed
    • designSpecHeight parameter is removed from initScaledSettings
  • 1.1.1
    • FontScale is calculated with design spec width length not design spec diagonal length
  • 1.1.0 (Sorry to API changes)
    • dimenScaled() is renamed to scaled()
    • New number type augmentation dimenWidthScaled(), dimenHeightScaled()
    • Add simple alias for functions d(), f(), w(), h()
  • 1.0.6
    • Apply round for fixing showing weird line because of floating number dimension

feel free your fork or any PR! Thanks