Skip to content

Commit daadd27

Browse files
committed
feat: addStyleSHeet
1 parent 329ff54 commit daadd27

5 files changed

Lines changed: 60 additions & 14 deletions

File tree

cpp/HybridStyleRegistry.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace margelo::nitro::cssnitro {
2929

3030
void set(const std::string &className, const std::vector<HybridStyleRule> &styleRule);
3131

32+
void addStyleSheet(const HybridStyleSheet &stylesheet);
33+
3234
Declarations
3335
getDeclarations(const std::string &componentId, const std::string &classNames,
3436
const std::string &variableScope, const std::string &containerScope);
@@ -100,6 +102,10 @@ namespace margelo::nitro::cssnitro {
100102
impl_->set(className, styleRule);
101103
}
102104

105+
void HybridStyleRegistry::addStyleSheet(const HybridStyleSheet &stylesheet) {
106+
impl_->addStyleSheet(stylesheet);
107+
}
108+
103109
Declarations HybridStyleRegistry::getDeclarations(const std::string &componentId,
104110
const std::string &classNames,
105111
const std::string &variableScope,
@@ -183,6 +189,24 @@ namespace margelo::nitro::cssnitro {
183189
}
184190
}
185191

192+
void HybridStyleRegistry::Impl::addStyleSheet(const HybridStyleSheet &stylesheet) {
193+
// Create an Effect batch to process all style updates together
194+
reactnativecss::Effect::batch([this, &stylesheet]() {
195+
// If the key "s" exists, loop over every entry
196+
if (stylesheet.s.has_value()) {
197+
const auto &styles = stylesheet.s.value();
198+
for (const auto &entry: styles) {
199+
// entry is a tuple<string, HybridStyleRule>
200+
// entry[0] is the className, entry[1] is the styleRule
201+
const std::string &className = std::get<0>(entry);
202+
const std::vector<HybridStyleRule> &styleRule = std::get<1>(entry);
203+
204+
// Call set with a vector containing the single styleRule
205+
set(className, styleRule);
206+
}
207+
}
208+
});
209+
}
186210

187211
Declarations HybridStyleRegistry::Impl::getDeclarations(const std::string &componentId,
188212
const std::string &classNames,

cpp/HybridStyleRegistry.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace margelo::nitro::cssnitro {
3939
void set(const std::string &className,
4040
const std::vector<HybridStyleRule> &styleRule) override;
4141

42+
void addStyleSheet(const HybridStyleSheet &stylesheet) override;
43+
4244
Declarations getDeclarations(const std::string &componentId, const std::string &classNames,
4345
const std::string &variableScope,
4446
const std::string &containerScope) override;

cpp/VariableContext.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <string>
44
#include <unordered_map>
55
#include <memory>
6-
#include <NitroModules/AnyValue.hpp>
6+
#include <NitroModules/AnyMap.hpp>
77
#include "Observable.hpp"
88
#include "Effect.hpp"
99

example/src/App.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@ import { StyleSheet, View } from "react-native";
33
import { multiply, StyleRegistry } from "react-native-css-nitro";
44
import { Text } from "react-native-css-nitro/components/Text";
55

6-
StyleRegistry.set("text-red-500", [
7-
{ s: [], d: [{ color: "red" }] },
8-
{ s: [], d: [{ color: "green" }], m: { orientation: ["=", "landscape"] } },
9-
{ s: [], d: [{ color: ["fn", "test", []] }] },
10-
]);
6+
StyleRegistry.addStyleSheet({
7+
s: [
8+
[
9+
"text-red-500",
10+
[
11+
{ s: [], d: [{ color: "red" }] },
12+
{
13+
s: [],
14+
d: [{ color: "green" }],
15+
m: { orientation: ["=", "landscape"] },
16+
},
17+
{ s: [], d: [{ color: ["fn", "test", []] }] },
18+
],
19+
],
20+
],
21+
});
1122

1223
export default function App() {
1324
return (

src/specs/StyleRegistry/HybridStyleRegistry.nitro.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type HybridStyleRegistry = StyleRegistry & RawStyleRegistry;
77
export interface StyleRegistry
88
extends HybridObject<{ ios: "c++"; android: "c++" }> {
99
set(className: string, styleRule: HybridStyleRule[]): void;
10+
addStyleSheet(stylesheet: HybridStyleSheet): void;
1011
getDeclarations(
1112
componentId: string,
1213
classNames: string,
@@ -100,11 +101,26 @@ export type StyleConfig = [
100101
];
101102
export type StyleConfigNativeStyleToProp = [string, string[]];
102103

104+
/****************************** StyleSheet *******************************/
105+
106+
export interface HybridStyleSheet {
107+
/** rem */
108+
r?: number;
109+
/** StyleRuleSets */
110+
s?: (readonly [string, HybridStyleRule[]])[];
111+
// /** KeyFrames */
112+
// k?: Animation[];
113+
// /** Root Variables */
114+
vr?: AnyMap;
115+
// /** Universal Variables */
116+
// vu?: RootVariables;
117+
}
118+
103119
/****************************** StyleRule *******************************/
104120

105121
interface HybridStyleRule {
106122
s: SpecificityArray;
107-
v?: HybridVariableDescriptor[];
123+
v?: AnyMap;
108124

109125
/** Declarations */
110126
d?: [AnyMap] | [AnyMap, AnyMap?];
@@ -114,10 +130,3 @@ interface HybridStyleRule {
114130
}
115131

116132
type SpecificityArray = number[];
117-
118-
/****************************** Variables *******************************/
119-
120-
type HybridVariableDescriptor = [
121-
string,
122-
AnyMap | AnyMap[] | string | number | boolean,
123-
];

0 commit comments

Comments
 (0)