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

feat: Extensions to makeStrictStyles #310

Merged
Merged
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
15 changes: 13 additions & 2 deletions packages/teams-components/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ import {
renderButton_unstable,
type ButtonProps as ButtonPropsBase,
Tooltip,
GriffelStyle,
} from '@fluentui/react-components';
import { validateIconButton, validateMenuButton } from './validateProps';
import { type StrictCssClass, validateStrictClasses } from '../../strictStyles';
import {
type StrictCssClass,
validateStrictClasses,
type DefaultStrictStyles,
} from '../../strictStyles';
import { type StrictSlot } from '../../strictSlot';

export type ButtonStrictOverrides = Pick<
GriffelStyle,
'maxWidth' | 'minWidth'
> &
DefaultStrictStyles;

export interface ButtonProps
extends Pick<
ButtonPropsBase,
Expand All @@ -22,7 +33,7 @@ export interface ButtonProps
| 'disabledFocusable'
> {
appearance?: 'transparent' | 'primary';
className?: StrictCssClass;
className?: StrictCssClass<ButtonStrictOverrides>;
icon?: StrictSlot;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
title?: StrictSlot;
Expand Down
2 changes: 1 addition & 1 deletion packages/teams-components/src/strictStyles/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { makeStrictStyles } from './makeStrictStyles';
export { mergeStrictClasses } from './mergeStrictClasses';
export { validateStrictClasses } from './validateStrictClasses';
export type { StrictCssClass } from './types';
export type { StrictCssClass, DefaultStrictStyles } from './types';
39 changes: 31 additions & 8 deletions packages/teams-components/src/strictStyles/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import * as React from 'react';
import type { STRICT_SYMBOL } from './STRICT_SYMBOL';
import type { GriffelStyle } from '@fluentui/react-components';

export interface StrictCssClass {
export interface StrictCssClass<TExtension = DefaultStrictStyles> {
/**
* @returns CSS class string
*/
toString: () => string;
DO_NOT_USE_OR_YOU_WILL_BE_FIRED: typeof STRICT_SYMBOL;
/**
* This field is only used to allow style extensions for components
*/
overrideFilter?: TExtension;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you could use a symbol to hide this property

}

/**
* Allow list for CSS properties - add to this cautiously
*/
export type StrictStyles = Pick<
export type DefaultStrictStyles = Pick<
GriffelStyle,
| 'marginInline'
| 'marginBlock'
Expand All @@ -25,9 +27,30 @@ export type StrictStyles = Pick<
| 'alignSelf'
>;

export type MakeStrictStyles = <Slots extends string>(
styles: Record<Slots, StrictStyles>
) => () => Record<Slots, StrictCssClass>;
/**
* Allow list for CSS properties - add to this cautiously
*/
export type StrictStyles<TExtension = DefaultStrictStyles> =
DefaultStrictStyles & TExtension;

type BaseComponenType = React.JSXElementConstructor<{
className?: StrictCssClass;
}>;

type ExtractStrictStyleOverrides<TComponent extends BaseComponenType> =
NonNullable<
NonNullable<React.ComponentProps<TComponent>['className']>['overrideFilter']
>;

export type MakeStrictStyles = <
TComponent extends BaseComponenType,
Slots extends string = string
>(
styles: Record<Slots, StrictStyles<ExtractStrictStyleOverrides<TComponent>>>
) => () => Record<
Slots,
StrictCssClass<ExtractStrictStyleOverrides<TComponent>>
>;

export type MergeStrictClasses = (
...strictClasses: StrictCssClass[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { STRICT_SYMBOL } from './STRICT_SYMBOL';
import type { StrictCssClass } from './types';

export const validateStrictClasses = (
className?: StrictCssClass | false | undefined
export const validateStrictClasses = <TExtension>(
className?: StrictCssClass<TExtension> | false | undefined
) => {
if (!className) {
return;
Expand Down