Skip to content

Commit e886132

Browse files
authored
[ui][ios] Add variable color support to Image component (expo#39852)
1 parent e0637c1 commit e886132

File tree

7 files changed

+93
-2
lines changed

7 files changed

+93
-2
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Host, HStack, Image, Slider, VStack } from '@expo/ui/swift-ui';
2+
import * as React from 'react';
3+
import { useState } from 'react';
4+
5+
import { ScrollPage, Section } from '../../components/Page';
6+
7+
export default function ImageScreen() {
8+
const [variableValue, setVariableValue] = useState(0.5);
9+
10+
return (
11+
<ScrollPage>
12+
<Section title="Basic SF Symbols">
13+
<Host matchContents>
14+
<HStack spacing={16}>
15+
<Image systemName="heart" size={32} />
16+
<Image systemName="star" size={32} />
17+
<Image systemName="folder" size={32} />
18+
<Image systemName="gear" size={32} />
19+
</HStack>
20+
</Host>
21+
</Section>
22+
23+
<Section title="Tinted SF Symbols">
24+
<Host matchContents>
25+
<HStack spacing={16}>
26+
<Image systemName="heart" size={32} color="red" />
27+
<Image systemName="star" size={32} color="blue" />
28+
<Image systemName="folder" size={32} color="green" />
29+
<Image systemName="gear" size={32} color="yellow" />
30+
</HStack>
31+
</Host>
32+
</Section>
33+
34+
<Section title="Variable Color">
35+
<Host matchContents>
36+
<VStack alignment="center" spacing={8}>
37+
<HStack spacing={8}>
38+
<Image systemName="cellularbars" size={32} variableValue={variableValue} />
39+
<Image systemName="wifi" size={32} variableValue={variableValue} />
40+
<Image
41+
systemName="antenna.radiowaves.left.and.right"
42+
size={32}
43+
variableValue={variableValue}
44+
/>
45+
</HStack>
46+
<Slider min={0} max={1} value={variableValue} onValueChange={setVariableValue} />
47+
</VStack>
48+
</Host>
49+
</Section>
50+
</ScrollPage>
51+
);
52+
}
53+
54+
ImageScreen.navigationOptions = {
55+
title: 'Image (SwiftUI)',
56+
};

apps/native-component-list/src/screens/UI/UIScreen.ios.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ export const UIScreens = [
170170
return optionalRequire(() => require('./ShapesScreen'));
171171
},
172172
},
173+
{
174+
name: 'Image component',
175+
route: 'ui/image',
176+
options: {},
177+
getComponent() {
178+
return optionalRequire(() => require('./ImageScreen'));
179+
},
180+
},
173181
];
174182

175183
export default function UIScreen() {

packages/expo-ui/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### 🎉 New features
88

9+
- [iOS] Add `variableValue` prop to Image component for SF Symbols with variable color support ([#39852](https://github.com/expo/expo/pull/39852) by [@morellodev](https://github.com/morellodev))
910
- [iOS] Adds `Rectangle`, `RoundedRectangle`, `UnevenRoundedRectangle`, `Circle`, `Ellipse`, `Capsule` shape components and `fill` modifier ([#39793](https://github.com/expo/expo/pull/39793) by [@nishan](https://github.com/intergalacticspacehighway))
1011
- [iOS] Add `ignoreSafeArea` modifier ([#39804](https://github.com/expo/expo/pull/39804) by [@nishan](https://github.com/intergalacticspacehighway))
1112
- [iOS] Add SF symbol typings ([#39802](https://github.com/expo/expo/pull/39802) by [@nishan](https://github.com/intergalacticspacehighway))

packages/expo-ui/build/swift-ui/Image/index.d.ts

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/expo-ui/build/swift-ui/Image/index.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/expo-ui/ios/ImageView.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal final class ImageViewProps: ExpoSwiftUI.ViewProps, CommonViewModifierPr
1313
@Field var systemName: String = ""
1414
@Field var size: Double?
1515
@Field var color: Color?
16+
@Field var variableValue: Double?
1617
@Field var useTapGesture: Bool?
1718
var onTap = EventDispatcher()
1819
}
@@ -21,7 +22,16 @@ internal struct ImageView: ExpoSwiftUI.View {
2122
@ObservedObject var props: ImageViewProps
2223

2324
var body: some View {
24-
Image(systemName: props.systemName)
25+
let image: Image
26+
27+
if #available(iOS 16.0, tvOS 16.0, *) {
28+
image = Image(systemName: props.systemName, variableValue: props.variableValue)
29+
} else {
30+
image = Image(systemName: props.systemName)
31+
}
32+
33+
return
34+
image
2535
.font(.system(size: CGFloat(props.size ?? 24)))
2636
.foregroundColor(props.color)
2737
.modifier(CommonViewModifiers(props: props))

packages/expo-ui/src/swift-ui/Image/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export interface ImageProps extends CommonViewModifierProps {
2020
* Can be a color name like '#ff00ff', 'red', 'blue', etc.
2121
*/
2222
color?: string;
23+
/**
24+
* The variable value for SF Symbols with variable color support.
25+
* Can be a number between 0.0 and 1.0.
26+
* Only works with SF Symbols that support variable color.
27+
*
28+
* Requires iOS 16.0+.
29+
*/
30+
variableValue?: number;
2331

2432
/**
2533
* Callback triggered when the view is pressed.

0 commit comments

Comments
 (0)