react-native-nano-icons 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -94,8 +94,8 @@ class NanoIconViewManager :
94
94
  view.setFontSize(value)
95
95
  }
96
96
 
97
- @ReactProp(name = "allowFontScaling", defaultBoolean = true)
98
- override fun setAllowFontScaling(view: NanoIconView, value: Boolean) {
97
+ @ReactProp(name = "fontScale", defaultFloat = 1f)
98
+ override fun setFontScale(view: NanoIconView, value: Float) {
99
99
  }
100
100
 
101
101
  @ReactProp(name = "advanceWidth", defaultInt = 0)
@@ -0,0 +1,117 @@
1
+ #include "NanoIconProps.h"
2
+
3
+ #include <react/renderer/core/PropsMacros.h>
4
+ #include <react/renderer/core/propsConversions.h>
5
+ #include <yoga/style/StyleSizeLength.h>
6
+
7
+ namespace facebook::react {
8
+
9
+ namespace {
10
+
11
+ bool rawPropIsSet(const RawProps &rawProps, const char *name, bool sourceValue) {
12
+ const auto *rawValue = rawProps.at(name, nullptr, nullptr);
13
+ if (rawValue == nullptr) {
14
+ return sourceValue;
15
+ }
16
+ return rawValue->hasValue();
17
+ }
18
+
19
+ } // namespace
20
+
21
+ NanoIconProps::NanoIconProps(
22
+ const PropsParserContext &context,
23
+ const NanoIconProps &sourceProps,
24
+ const RawProps &rawProps)
25
+ : ViewProps(context, sourceProps, rawProps),
26
+ fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})),
27
+ codepoints(convertRawProp(context, rawProps, "codepoints", sourceProps.codepoints, {})),
28
+ colors(convertRawProp(context, rawProps, "colors", sourceProps.colors, {})),
29
+ color(convertRawProp(context, rawProps, "color", sourceProps.color, {})),
30
+ tintLayers(convertRawProp(context, rawProps, "tintLayers", sourceProps.tintLayers, {})),
31
+ size(convertRawProp(context, rawProps, "size", sourceProps.size, {0.0})),
32
+ fontScale(convertRawProp(context, rawProps, "fontScale", sourceProps.fontScale, {1.0})),
33
+ advanceWidth(convertRawProp(context, rawProps, "advanceWidth", sourceProps.advanceWidth, {0})),
34
+ unitsPerEm(convertRawProp(context, rawProps, "unitsPerEm", sourceProps.unitsPerEm, {0})),
35
+ hasStyleWidth(rawPropIsSet(rawProps, "width", sourceProps.hasStyleWidth)),
36
+ hasStyleHeight(rawPropIsSet(rawProps, "height", sourceProps.hasStyleHeight)) {
37
+ applyGlyphDimensions();
38
+ }
39
+
40
+ void NanoIconProps::setProp(
41
+ const PropsParserContext &context,
42
+ RawPropsPropNameHash hash,
43
+ const char *propName,
44
+ const RawValue &value) {
45
+ ViewProps::setProp(context, hash, propName, value);
46
+ switch (hash) {
47
+ case CONSTEXPR_RAW_PROPS_KEY_HASH("width"):
48
+ hasStyleWidth = value.hasValue();
49
+ applyGlyphDimensions();
50
+ break;
51
+ case CONSTEXPR_RAW_PROPS_KEY_HASH("height"):
52
+ hasStyleHeight = value.hasValue();
53
+ applyGlyphDimensions();
54
+ break;
55
+ }
56
+ }
57
+
58
+ void NanoIconProps::applyGlyphDimensions() {
59
+ const Float height = size * fontScale;
60
+ const Float width = unitsPerEm > 0
61
+ ? height * static_cast<Float>(advanceWidth) / static_cast<Float>(unitsPerEm)
62
+ : height;
63
+ if (!hasStyleWidth) {
64
+ yogaStyle.setDimension(yoga::Dimension::Width, yoga::StyleSizeLength::points(width));
65
+ }
66
+ if (!hasStyleHeight) {
67
+ yogaStyle.setDimension(yoga::Dimension::Height, yoga::StyleSizeLength::points(height));
68
+ }
69
+ }
70
+
71
+ #ifdef RN_SERIALIZABLE_STATE
72
+ ComponentName NanoIconProps::getDiffPropsImplementationTarget() const {
73
+ return "NanoIconView";
74
+ }
75
+
76
+ folly::dynamic NanoIconProps::getDiffProps(const Props *prevProps) const {
77
+ static const auto defaultProps = NanoIconProps();
78
+ const NanoIconProps *oldProps = prevProps == nullptr
79
+ ? &defaultProps
80
+ : static_cast<const NanoIconProps *>(prevProps);
81
+ if (this == oldProps) {
82
+ return folly::dynamic::object();
83
+ }
84
+ folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
85
+
86
+ if (fontFamily != oldProps->fontFamily) {
87
+ result["fontFamily"] = fontFamily;
88
+ }
89
+ if (codepoints != oldProps->codepoints) {
90
+ result["codepoints"] = toDynamic(codepoints);
91
+ }
92
+ if (colors != oldProps->colors) {
93
+ result["colors"] = toDynamic(colors);
94
+ }
95
+ if (color != oldProps->color) {
96
+ result["color"] = *color;
97
+ }
98
+ if (tintLayers != oldProps->tintLayers) {
99
+ result["tintLayers"] = toDynamic(tintLayers);
100
+ }
101
+ if ((size != oldProps->size) && !(std::isnan(size) && std::isnan(oldProps->size))) {
102
+ result["size"] = size;
103
+ }
104
+ if ((fontScale != oldProps->fontScale) && !(std::isnan(fontScale) && std::isnan(oldProps->fontScale))) {
105
+ result["fontScale"] = fontScale;
106
+ }
107
+ if (advanceWidth != oldProps->advanceWidth) {
108
+ result["advanceWidth"] = advanceWidth;
109
+ }
110
+ if (unitsPerEm != oldProps->unitsPerEm) {
111
+ result["unitsPerEm"] = unitsPerEm;
112
+ }
113
+ return result;
114
+ }
115
+ #endif
116
+
117
+ } // namespace facebook::react
@@ -0,0 +1,58 @@
1
+ #pragma once
2
+
3
+ #include <react/renderer/components/view/ViewProps.h>
4
+ #include <react/renderer/core/PropsParserContext.h>
5
+ #include <react/renderer/graphics/Color.h>
6
+ #include <vector>
7
+
8
+ namespace facebook::react {
9
+
10
+ // Why this class exists instead of the codegen NanoIconViewProps.
11
+ // 1. The icon box must be an explicit width/height in the Yoga style. A parent
12
+ // row with the default alignItems: 'stretch' stretches every child to the
13
+ // row height unless the child has explicit dimensions; a measure function
14
+ // does not count, Yoga ignores it in the stretch pass.
15
+ // 2. Those dimensions must be stored on the Props object. On every update RN
16
+ // copies the style from Props onto the Yoga node, so values written to the
17
+ // node directly would be wiped and cause a re-layout each time.
18
+ // 3. The codegen class only parses what JS sent and is final, so it cannot be
19
+ // extended. This class repeats that parsing and adds one step: compute
20
+ // width and height from size, fontScale and the glyph advance ratio, and
21
+ // store them in the style unless the user style already sets them.
22
+ class NanoIconProps final : public ViewProps {
23
+ public:
24
+ NanoIconProps() = default;
25
+ NanoIconProps(
26
+ const PropsParserContext &context,
27
+ const NanoIconProps &sourceProps,
28
+ const RawProps &rawProps);
29
+
30
+ void setProp(
31
+ const PropsParserContext &context,
32
+ RawPropsPropNameHash hash,
33
+ const char *propName,
34
+ const RawValue &value);
35
+
36
+ std::string fontFamily{};
37
+ std::vector<int> codepoints{};
38
+ std::vector<SharedColor> colors{};
39
+ SharedColor color{};
40
+ std::vector<int> tintLayers{};
41
+ Float size{0.0};
42
+ Float fontScale{1.0};
43
+ int advanceWidth{0};
44
+ int unitsPerEm{0};
45
+ bool hasStyleWidth{false};
46
+ bool hasStyleHeight{false};
47
+
48
+ #ifdef RN_SERIALIZABLE_STATE
49
+ ComponentName getDiffPropsImplementationTarget() const override;
50
+
51
+ folly::dynamic getDiffProps(const Props *prevProps) const override;
52
+ #endif
53
+
54
+ private:
55
+ void applyGlyphDimensions();
56
+ };
57
+
58
+ } // namespace facebook::react
@@ -1,46 +1,7 @@
1
1
  #include "NanoIconViewShadowNode.h"
2
2
 
3
- #include <react/renderer/core/LayoutConstraints.h>
4
- #include <react/renderer/core/LayoutContext.h>
5
-
6
3
  namespace facebook::react {
7
4
 
8
5
  extern const char NanoIconViewComponentName[] = "NanoIconView";
9
6
 
10
- Size NanoIconViewShadowNode::measureContent(
11
- const LayoutContext &layoutContext,
12
- const LayoutConstraints &layoutConstraints) const {
13
- const auto &props = getConcreteProps();
14
- const Float height =
15
- props.size * (props.allowFontScaling ? layoutContext.fontSizeMultiplier : 1);
16
- const Float width = props.unitsPerEm > 0
17
- ? height * static_cast<Float>(props.advanceWidth) / static_cast<Float>(props.unitsPerEm)
18
- : height;
19
- return layoutConstraints.clamp({width, height});
20
- }
21
-
22
- bool NanoIconViewShadowNode::shouldNewRevisionDirtyMeasurement(
23
- const ShadowNode & /*sourceShadowNode*/,
24
- const ShadowNodeFragment & /*fragment*/) const {
25
- return false;
26
- }
27
-
28
- void NanoIconViewShadowNode::completeClone(
29
- const ShadowNode &sourceShadowNode,
30
- const ShadowNodeFragment &fragment) {
31
- ConcreteViewShadowNode::completeClone(sourceShadowNode, fragment);
32
- if (fragment.props == nullptr) {
33
- return;
34
- }
35
- const auto &oldProps =
36
- static_cast<const NanoIconViewProps &>(*sourceShadowNode.getProps());
37
- const auto &newProps = getConcreteProps();
38
- if (oldProps.size != newProps.size ||
39
- oldProps.allowFontScaling != newProps.allowFontScaling ||
40
- oldProps.advanceWidth != newProps.advanceWidth ||
41
- oldProps.unitsPerEm != newProps.unitsPerEm) {
42
- dirtyLayout();
43
- }
44
- }
45
-
46
7
  } // namespace facebook::react
@@ -1,7 +1,7 @@
1
1
  #pragma once
2
2
 
3
3
  #include <react/renderer/components/RNNanoIconsSpec/EventEmitters.h>
4
- #include <react/renderer/components/RNNanoIconsSpec/Props.h>
4
+ #include <react/renderer/components/RNNanoIconsSpec/NanoIconProps.h>
5
5
  #include <react/renderer/components/view/ConcreteViewShadowNode.h>
6
6
  #include <jsi/jsi.h>
7
7
 
@@ -12,7 +12,7 @@ JSI_EXPORT extern const char NanoIconViewComponentName[];
12
12
  class JSI_EXPORT NanoIconViewShadowNode final
13
13
  : public ConcreteViewShadowNode<
14
14
  NanoIconViewComponentName,
15
- NanoIconViewProps,
15
+ NanoIconProps,
16
16
  NanoIconViewEventEmitter> {
17
17
  public:
18
18
  using ConcreteViewShadowNode::ConcreteViewShadowNode;
@@ -20,22 +20,8 @@ class JSI_EXPORT NanoIconViewShadowNode final
20
20
  static ShadowNodeTraits BaseTraits() {
21
21
  auto traits = ConcreteViewShadowNode::BaseTraits();
22
22
  traits.set(ShadowNodeTraits::Trait::LeafYogaNode);
23
- traits.set(ShadowNodeTraits::Trait::MeasurableYogaNode);
24
23
  return traits;
25
24
  }
26
-
27
- Size measureContent(
28
- const LayoutContext &layoutContext,
29
- const LayoutConstraints &layoutConstraints) const override;
30
-
31
- void completeClone(
32
- const ShadowNode &sourceShadowNode,
33
- const ShadowNodeFragment &fragment) override;
34
-
35
- protected:
36
- bool shouldNewRevisionDirtyMeasurement(
37
- const ShadowNode &sourceShadowNode,
38
- const ShadowNodeFragment &fragment) const override;
39
25
  };
40
26
 
41
27
  } // namespace facebook::react
@@ -4,7 +4,7 @@
4
4
  #import <React/RCTConversions.h>
5
5
  #import <React/RCTFabricComponentsPlugins.h>
6
6
  #import <react/renderer/components/RNNanoIconsSpec/NanoIconViewComponentDescriptor.h>
7
- #import <react/renderer/components/RNNanoIconsSpec/Props.h>
7
+ #import <react/renderer/components/RNNanoIconsSpec/NanoIconProps.h>
8
8
  #import "NanoIconInlineBaseline.h"
9
9
 
10
10
  using namespace facebook::react;
@@ -77,7 +77,7 @@ static void NanoIconTrackLiveView(NanoIconView *view) {
77
77
 
78
78
  - (instancetype)initWithFrame:(CGRect)frame {
79
79
  if (self = [super initWithFrame:frame]) {
80
- static const auto defaultProps = std::make_shared<const NanoIconViewProps>();
80
+ static const auto defaultProps = std::make_shared<const NanoIconProps>();
81
81
  _props = defaultProps;
82
82
  self.opaque = NO;
83
83
  self.backgroundColor = [UIColor clearColor];
@@ -314,8 +314,8 @@ static void NanoIconTrackLiveView(NanoIconView *view) {
314
314
  }
315
315
 
316
316
  - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps {
317
- const auto &oldViewProps = static_cast<const NanoIconViewProps &>(*_props);
318
- const auto &newViewProps = static_cast<const NanoIconViewProps &>(*props);
317
+ const auto &oldViewProps = static_cast<const NanoIconProps &>(*_props);
318
+ const auto &newViewProps = static_cast<const NanoIconProps &>(*props);
319
319
 
320
320
  BOOL fontChanged = NO;
321
321
  BOOL needsRedraw = NO;
@@ -406,7 +406,7 @@ static void NanoIconTrackLiveView(NanoIconView *view) {
406
406
  if (_font || _fontFamily.length == 0) return;
407
407
  _font = NanoIconResolveFont(_fontFamily, _fontSize);
408
408
  if (!_font) return;
409
- const auto &viewProps = static_cast<const NanoIconViewProps &>(*_props);
409
+ const auto &viewProps = static_cast<const NanoIconProps &>(*_props);
410
410
  [self _mapGlyphs:viewProps.codepoints];
411
411
  _metricsValid = NO;
412
412
  [self _setNeedsRedraw];
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  import { memo, useMemo } from 'react';
4
- import { PixelRatio, UIManager, View } from 'react-native';
4
+ import { UIManager, View } from 'react-native';
5
5
  import { shallowEqualColor } from "./utils/shallowEqualColor.js";
6
+ import { useFontScale } from "./utils/fontScale.js";
6
7
  import { validateLayerColor } from "./utils/validateLayerColor.js";
7
8
  import { DEFAULT_ICON_SIZE, resolveGlyphEntry, createLayerColorResolver, tintableLayers } from "./utils/glyphRuntime.js";
8
9
  import NanoIconViewNative from './specs/NanoIconViewNativeComponent';
@@ -83,7 +84,7 @@ export function createIconSet(glyphMap, font) {
83
84
  testID,
84
85
  ref
85
86
  }) => {
86
- const fontScale = allowFontScaling ? PixelRatio.getFontScale() : 1;
87
+ const fontScale = useFontScale(allowFontScaling);
87
88
  const [adv, layers] = resolveGlyphEntry(glyphMap, name);
88
89
  const scaledSize = size * fontScale;
89
90
  const width = adv / unitsPerEm * scaledSize;
@@ -124,7 +125,7 @@ export function createIconSet(glyphMap, font) {
124
125
  colors: layerColors,
125
126
  tintLayers: tintableLayers(layers, tintMode),
126
127
  size: size,
127
- allowFontScaling: allowFontScaling,
128
+ fontScale: fontScale,
128
129
  advanceWidth: adv,
129
130
  unitsPerEm: unitsPerEm,
130
131
  style: style,
@@ -9,7 +9,7 @@ export interface NativeProps extends ViewProps {
9
9
  color?: ColorValue;
10
10
  tintLayers: ReadonlyArray<Int32>;
11
11
  size: Float;
12
- allowFontScaling: boolean;
12
+ fontScale: Float;
13
13
  advanceWidth: Int32;
14
14
  unitsPerEm: Int32;
15
15
  }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ import { useSyncExternalStore } from 'react';
4
+ import { Dimensions, PixelRatio } from 'react-native';
5
+ function subscribe(onChange) {
6
+ const subscription = Dimensions.addEventListener('change', onChange);
7
+ return () => subscription.remove();
8
+ }
9
+ function getFontScale() {
10
+ return PixelRatio.getFontScale();
11
+ }
12
+ export function useFontScale(enabled) {
13
+ const fontScale = useSyncExternalStore(subscribe, getFontScale);
14
+ return enabled ? fontScale : 1;
15
+ }
@@ -7,7 +7,7 @@ export interface NativeProps extends ViewProps {
7
7
  color?: ColorValue;
8
8
  tintLayers: ReadonlyArray<Int32>;
9
9
  size: Float;
10
- allowFontScaling: boolean;
10
+ fontScale: Float;
11
11
  advanceWidth: Int32;
12
12
  unitsPerEm: Int32;
13
13
  }
@@ -0,0 +1,2 @@
1
+ export declare function useFontScale(enabled: boolean): number;
2
+ //# sourceMappingURL=fontScale.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nano-icons",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "High-performance icon rendering for React Native & Expo.",
5
5
  "react-native": "./lib/module/index.js",
6
6
  "source": "src/index.ts",
@@ -9,7 +9,7 @@ export interface NativeProps extends ViewProps {
9
9
  color?: ColorValue;
10
10
  tintLayers: ReadonlyArray<Int32>;
11
11
  size: Float;
12
- allowFontScaling: boolean;
12
+ fontScale: Float;
13
13
  advanceWidth: Int32;
14
14
  unitsPerEm: Int32;
15
15
  }