Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1386 from crupest/dev
Develop.
- Loading branch information
Showing
8 changed files
with
105 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,10 @@ | ||
import { TFunction } from "i18next"; | ||
|
||
// This error is thrown when ui goes wrong with bad logic. | ||
// Such as a variable should not be null, but it does. | ||
// This error should never occur. If it does, it indicates there is some logic bug in codes. | ||
export class UiLogicError extends Error {} | ||
|
||
export type I18nText = | ||
| string | ||
| { type: "custom"; value: string } | ||
| { type: "i18n"; value: string }; | ||
|
||
export function convertI18nText(text: I18nText, t: TFunction): string; | ||
export function convertI18nText( | ||
text: I18nText | null | undefined, | ||
t: TFunction | ||
): string | null; | ||
export function convertI18nText( | ||
text: I18nText | null | undefined, | ||
t: TFunction | ||
): string | null { | ||
if (text == null) { | ||
return null; | ||
} else if (typeof text === "string") { | ||
return t(text); | ||
} else if (text.type === "i18n") { | ||
return t(text.value); | ||
} else { | ||
return text.value; | ||
} | ||
} | ||
|
||
export const highlightTimelineUsername = "crupest"; | ||
|
||
export type { I18nText } from "./i18n"; | ||
export { c, convertI18nText } from "./i18n"; | ||
export { default as useC } from "./utilities/hooks/use-c"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useTranslation } from "react-i18next"; | ||
import { C, createC } from "../../i18n"; | ||
|
||
export default function useC(ns?: string): C { | ||
const { t } = useTranslation(ns); | ||
return createC(t); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,47 @@ | ||
import * as React from "react"; | ||
import { ComponentPropsWithoutRef, Ref } from "react"; | ||
import classNames from "classnames"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { convertI18nText, I18nText } from "@/common"; | ||
import { I18nText, useC } from "@/common"; | ||
import { PaletteColorType } from "@/palette"; | ||
|
||
import "./Button.css"; | ||
|
||
function _Button( | ||
props: { | ||
color?: PaletteColorType; | ||
text?: I18nText; | ||
outline?: boolean; | ||
} & React.ComponentPropsWithoutRef<"button">, | ||
ref: React.ForwardedRef<HTMLButtonElement> | ||
): JSX.Element { | ||
const { t } = useTranslation(); | ||
interface ButtonProps extends ComponentPropsWithoutRef<"button"> { | ||
color?: PaletteColorType; | ||
text?: I18nText; | ||
outline?: boolean; | ||
buttonRef?: Ref<HTMLButtonElement> | null; | ||
} | ||
|
||
const { color, text, outline, className, children, ...otherProps } = props; | ||
export default function Button(props: ButtonProps) { | ||
const { | ||
buttonRef, | ||
color, | ||
text, | ||
outline, | ||
className, | ||
children, | ||
...otherProps | ||
} = props; | ||
|
||
if (text != null && children != null) { | ||
console.warn("You can't set both text and children props."); | ||
} | ||
|
||
const c = useC(); | ||
|
||
return ( | ||
<button | ||
ref={ref} | ||
ref={buttonRef} | ||
className={classNames( | ||
"cru-" + (color ?? "primary"), | ||
"cru-button", | ||
outline && "outline", | ||
className | ||
className, | ||
)} | ||
{...otherProps} | ||
> | ||
{text != null ? convertI18nText(text, t) : children} | ||
{text != null ? c(text) : children} | ||
</button> | ||
); | ||
} | ||
|
||
const Button = React.forwardRef(_Button); | ||
export default Button; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,37 @@ | ||
import * as React from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { ComponentPropsWithoutRef, Ref } from "react"; | ||
import classNames from "classnames"; | ||
|
||
import { convertI18nText, I18nText } from "@/common"; | ||
import { I18nText, useC } from "@/common"; | ||
import { PaletteColorType } from "@/palette"; | ||
|
||
import "./FlatButton.css"; | ||
|
||
function _FlatButton( | ||
props: { | ||
color?: PaletteColorType; | ||
text?: I18nText; | ||
} & React.ComponentPropsWithoutRef<"button">, | ||
ref: React.ForwardedRef<HTMLButtonElement> | ||
): React.ReactElement | null { | ||
const { t } = useTranslation(); | ||
interface FlatButtonProps extends ComponentPropsWithoutRef<"button"> { | ||
color?: PaletteColorType; | ||
text?: I18nText; | ||
buttonRef?: Ref<HTMLButtonElement> | null; | ||
} | ||
|
||
const { color, text, className, children, ...otherProps } = props; | ||
export default function FlatButton(props: FlatButtonProps) { | ||
const { color, text, className, children, buttonRef, ...otherProps } = props; | ||
|
||
if (text != null && children != null) { | ||
console.warn("You can't set both text and children props."); | ||
} | ||
|
||
const c = useC(); | ||
|
||
return ( | ||
<button | ||
ref={ref} | ||
ref={buttonRef} | ||
className={classNames( | ||
"cru-" + (color ?? "primary"), | ||
"cru-flat-button", | ||
className | ||
className, | ||
)} | ||
{...otherProps} | ||
> | ||
{text != null ? convertI18nText(text, t) : children} | ||
{text != null ? c(text) : children} | ||
</button> | ||
); | ||
} | ||
|
||
const FlatButton = React.forwardRef(_FlatButton); | ||
export default FlatButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters