Skip to content

Commit eee4370

Browse files
committed
update doc
1 parent 5f00f1d commit eee4370

File tree

2 files changed

+94
-18
lines changed

2 files changed

+94
-18
lines changed

docs/migration.md

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ To solve the above problems, you only need to implement a css() and cx() method.
343343

344344
## Guide
345345

346-
- Step1: Add new theme options for antd, eg:
346+
- Step1: Add new theme options for antd, eg:
347347

348348
```csharp
349349
services.AddAntDesign(option =>
@@ -355,7 +355,7 @@ To solve the above problems, you only need to implement a css() and cx() method.
355355
});
356356
```
357357

358-
- Step2: Gen style code from ts code.
358+
- Step2: Gen style code from ts code.
359359
```bash
360360
# use cssincs cli tool
361361
cssincs convert -c cssincs.json
@@ -370,8 +370,99 @@ To solve the above problems, you only need to implement a css() and cx() method.
370370
├── compact.cs
371371
├── token.cs
372372
```
373+
Each style's index file contains a default export method named `IndexDefault()`. The default naming rule is `filename + Default`, and you can modify the default generation rules by updating the configuration in cssincs.json.
374+
```json
375+
{
376+
"DefaultExportMethodName": "{file}Default",
377+
}
378+
```
373379

374-
- Step3: Register style for each component
380+
- Step3: Register style for each component.
381+
When registering styles, it is generally done in the `OnInitialized` method. However, for antd, it should be done in the `SetClassMap` method because the isolated `HashId` needs to be injected into the `ClassMapper`.
375382
```csharp
383+
// Button.razor.cs
384+
protected void SetClassMap()
385+
{
386+
var prefixName = "ant-btn";
387+
// 注入index.cs中的IndexDefault()方法
388+
var hashId = ButtonStyle.IndexDefault()(prefixName);
389+
// Add hashId
390+
ClassMapper
391+
.Add(prefixName)
392+
.Add(hashId)
393+
}
394+
```
376395

396+
- Step4: Merge tokens and render using CssInCSharp.
397+
The ButtonStyle.IndexDefault method calls `GenStyleHooks`, which needs to be implemented in antd. This method is used to merge the theme tokens and invoke the style injection method from CssInCsharp.
398+
```csharp
399+
internal static UseComponentStyleResult GenStyleHooks<T>(
400+
string componentName,
401+
Func<T, CSSObject> styleFunc,
402+
Func<T> getDefaultToken,
403+
GenOptions options = null
404+
)
405+
{
406+
// default token
407+
var defaultToken = ...
408+
// gen theme token
409+
var themeToken = option.ThemeAlgorithm();
410+
// component token
411+
var mergedToken = new T();
412+
mergedToken.Merge(defaultToken, themeToken);
413+
...
414+
// 这里就是调用CssInCsharp方法
415+
UseStyleRegister(new StyleInfo()
416+
{
417+
HashId = hash.HashId,
418+
TokenKey = hash.TokenKey,
419+
Path = new[] { "Shared", rootPrefixCls },
420+
StyleFn = () => new CSSObject { ["&"] = GlobalStyle.GenLinkStyle(token) },
421+
});
422+
}
423+
```
424+
Note: The theme options needs to be retrieved here. Since `IOptions` is injected clsss but the `GenStyleHooks` method is a static method, a static instance must be created to access it. eg:
425+
```csharp
426+
service.AddSingleton<ThemeService>((provider) =>
427+
{
428+
if (StyleUtil.ThemeService == default)
429+
{
430+
var options = provider.GetRequiredService<IOptions<ThemeOptions>>();
431+
StyleUtil.ThemeService = new ThemeService(options);
432+
}
433+
434+
return StyleUtil.ThemeService;
435+
});
436+
437+
public class ThemeService
438+
{
439+
/**
440+
* StyleHelper是CssInCsharp提供的静态类
441+
* 如果需要使用注入类可以使用StyleService
442+
* service.AddCssInCSharp();
443+
* public ThemeService(IOptions<ThemeOptions> options, StyleService styleService)
444+
* {
445+
* }
446+
*/
447+
public ThemeService(IOptions<ThemeOptions> options)
448+
{
449+
}
450+
451+
public UseComponentStyleResult UseStyle()
452+
{
453+
}
454+
}
455+
456+
internal static class StyleUtil
457+
{
458+
// static instance
459+
internal static ThemeService ThemeService { get;set; }
460+
461+
// hook method
462+
internal static UseComponentStyleResult GenStyleHooks<T>()
463+
{
464+
// call theme service
465+
return ThemeService.UseTyle();
466+
}
467+
}
377468
```

src/Styles/StyleHelper.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,16 @@ public static string Register(string style)
8080
return className;
8181
}
8282

83-
/// <summary>
84-
///
85-
/// </summary>
86-
/// <param name="css"></param>
8783
public static void UseStyleRegister(StyleInfo css)
8884
{
8985
Register(css);
9086
}
9187

92-
/// <summary>
93-
///
94-
/// </summary>
95-
/// <param name="css"></param>
96-
/// <returns>className</returns>
9788
public static string CSS(string css)
9889
{
9990
return Register(css);
10091
}
10192

102-
/// <summary>
103-
///
104-
/// </summary>
105-
/// <param name="className"></param>
106-
/// <param name="css"></param>
107-
/// <returns></returns>
10893
public static string CX(string className, CSSObject css)
10994
{
11095
return Register(className, css);

0 commit comments

Comments
 (0)