Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for color mode defined by color name prefix #218 #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/FigmaExport/Input/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ struct Params: Decodable {
let nameValidateRegexp: String?
let nameReplaceRegexp: String?
let useSingleFile: Bool?
let lightModePrefix: String?
let darkModePrefix: String?
let lightModeSuffix: String?
let darkModeSuffix: String?
let lightHCModePrefix: String?
let darkHCModePrefix: String?
let lightHCModeSuffix: String?
let darkHCModeSuffix: String?
}
Expand Down
38 changes: 24 additions & 14 deletions Sources/FigmaExport/Loaders/ColorsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,39 @@ final class ColorsLoader {
lightHC: [Color]?,
darkHC: [Color]?) {
let colors = try loadColors(fileId: figmaParams.lightFileId, filter: filter)

let darkSuffix = colorParams?.darkModeSuffix ?? "_dark"

let lightPrefix = colorParams?.lightModePrefix
let darkPrefix = colorParams?.darkModePrefix
let lightSuffix = colorParams?.lightModeSuffix
let darkSuffix = colorParams?.darkModeSuffix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To maintain backward compatibility

Suggested change
let darkSuffix = colorParams?.darkModeSuffix
let darkSuffix = colorParams?.darkModeSuffix ?? "_dark"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes no sense for me. Someone can use suffix, someone can use prefix, but not both. So if I only have suffixes in my Figma, how would I configure my figma-export with such default values?

let lightHCPrefix = colorParams?.lightHCModePrefix ?? "lightHC_"
let darkHCPrefix = colorParams?.darkHCModePrefix ?? "darkHC_"
let lightHCSuffix = colorParams?.lightHCModeSuffix ?? "_lightHC"
let darkHCSuffix = colorParams?.darkHCModeSuffix ?? "_darkHC"

let lightColors = colors
.filter {
!$0.name.hasSuffix(darkSuffix) &&
!$0.name.hasSuffix(lightHCSuffix) &&
!$0.name.hasSuffix(darkHCSuffix)
}
Comment on lines -45 to -50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you would like to specify light and dark colors in the same file, you can do so with the useSingleFile configuration option. You can then denote dark mode colors by adding a suffix like _dark.
(from https://github.com/RedMadRobot/figma-export?tab=readme-ov-file#design-requirements)

You must not delete this code. A color style list can contains colors which names doesn't contain prefixes and suffixes for light mode but contain "dark" suffix/prefix for dark mode. Example: "background", "foreground", "dark_background", "dark_foreground".

let darkColors = filteredColors(colors, suffix: darkSuffix)
let lightHCColors = filteredColors(colors, suffix: lightHCSuffix)
let darkHCColors = filteredColors(colors, suffix: darkHCSuffix)
let lightColors = filteredColors(colors, prefix: lightPrefix, suffix: lightSuffix)
let darkColors = filteredColors(colors, prefix: darkPrefix, suffix: darkSuffix)
let lightHCColors = filteredColors(colors, prefix: lightHCPrefix, suffix: lightHCSuffix)
let darkHCColors = filteredColors(colors, prefix: darkHCPrefix, suffix: darkHCSuffix)
return (lightColors, darkColors, lightHCColors, darkHCColors)
}

private func filteredColors(_ colors: [Color], suffix: String) -> [Color] {
private func filteredColors(_ colors: [Color], prefix: String?, suffix: String?) -> [Color] {
let filteredColors = colors
.filter { $0.name.hasSuffix(suffix) }
.filter {
if let prefix, let suffix {
return $0.name.hasPrefix(prefix) && $0.name.hasSuffix(suffix)
} else if let prefix {
return $0.name.hasPrefix(prefix)
} else if let suffix {
return $0.name.hasSuffix(suffix)
} else {
return true
}
}
.map { color -> Color in
var newColor = color
newColor.name = String(color.name.dropLast(suffix.count))
newColor.name = String(color.name.dropFirst(prefix?.count ?? 0).dropLast(suffix?.count ?? 0))
return newColor
}
return filteredColors
Expand Down
Loading