From b1576e5b1a407be91e8a8e48196d470e633cfd4b Mon Sep 17 00:00:00 2001 From: Biki-das Date: Tue, 16 Apr 2024 07:17:36 -0700 Subject: [PATCH] feat: Added PixelRatio API example (#44000) Summary: Added the [PixelRatio](https://reactnative.dev/docs/pixelratio) API to the RN tester app on the API list examples. ## Changelog: [General] [Added] - Add examples for the Pixel Ratio API, with all the available methods. Pull Request resolved: https://github.com/facebook/react-native/pull/44000 Test Plan: **iOS** https://github.com/facebook/react-native/assets/72331432/dd481916-a8df-4a62-8f63-61936d886d33 **android** https://github.com/facebook/react-native/assets/72331432/4d8d67dc-ddeb-4ca7-b61c-07522951f9ee Reviewed By: NickGerleman Differential Revision: D56135685 Pulled By: hoxyq fbshipit-source-id: f2f4eb9e31159a4a2c660d1a864b649e537916af --- .../examples/PixelRatio/PixelRatioExample.js | 201 ++++++++++++++++++ .../js/utils/RNTesterList.android.js | 5 + .../rn-tester/js/utils/RNTesterList.ios.js | 4 + 3 files changed, 210 insertions(+) create mode 100644 packages/rn-tester/js/examples/PixelRatio/PixelRatioExample.js diff --git a/packages/rn-tester/js/examples/PixelRatio/PixelRatioExample.js b/packages/rn-tester/js/examples/PixelRatio/PixelRatioExample.js new file mode 100644 index 00000000000..5dd49eb4240 --- /dev/null +++ b/packages/rn-tester/js/examples/PixelRatio/PixelRatioExample.js @@ -0,0 +1,201 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow strict-local + */ + +'use strict'; + +import React, {useState} from 'react'; +import { + Button, + PixelRatio, + StyleSheet, + Text, + TextInput, + View, +} from 'react-native'; + +function LayoutSizeToPixel() { + const [layoutDPSize, setLayoutDPSize] = useState(0); + const pixelSize = PixelRatio.getPixelSizeForLayoutSize( + layoutDPSize ? layoutDPSize : 0, + ); + + const handleDPInputChange = (changedText: string) => { + const layoutSize = parseInt(changedText, 10); + setLayoutDPSize(layoutSize); + }; + + return ( + + + + Layout Size(dp): + + + + Pixel Size: + {pixelSize}px + + + + ); +} + +function RoundToNearestPixel() { + const [layoutDPSizeText, setLayoutDPSizeText] = useState(''); + const layoutDPSize = parseFloat(layoutDPSizeText); + + const pixelSize = PixelRatio.roundToNearestPixel( + layoutDPSize ? layoutDPSize : 0, + ); + + const handleDPInputChange = (changedText: string) => { + setLayoutDPSizeText(changedText); + }; + + return ( + + + + Layout Size(dp): + + + + Nearest Layout Size: + {pixelSize}dp + + + + ); +} + +function GetPixelRatio() { + const [pixelDensity, setPixelDensity] = useState(0); + + const getPixelDensityCallback = () => { + setPixelDensity(PixelRatio.get()); + }; + + return ( + + +