Skip to content

Commit

Permalink
feat: adds customizeSelector to createCustomFocusIndicatorStyle method
Browse files Browse the repository at this point in the history
  • Loading branch information
bsunderhus committed Sep 25, 2023
1 parent 81d629f commit e33852b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat: adds suffixes to selectors on focus creator methods",
"packageName": "@fluentui/react-tabster",
"email": "bernardo.sunderhus@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import { Types } from 'tabster';
export function applyFocusVisiblePolyfill(scope: HTMLElement, targetWindow: Window): () => void;

// @public
export function createCustomFocusIndicatorStyle<TStyle extends GriffelStyle | GriffelResetStyle>(style: TStyle, { selector }?: CreateCustomFocusIndicatorStyleOptions): TStyle extends GriffelStyle ? GriffelStyle : GriffelResetStyle;
export function createCustomFocusIndicatorStyle<TStyle extends GriffelStyle | GriffelResetStyle>(style: TStyle, { selector: selectorType, customizeSelector, }?: CreateCustomFocusIndicatorStyleOptions): TStyle extends GriffelStyle ? GriffelStyle : GriffelResetStyle;

// @public (undocumented)
export interface CreateCustomFocusIndicatorStyleOptions {
customizeSelector?: (selector: string) => string;
// @deprecated
enableOutline?: boolean;
selector?: 'focus' | 'focus-within';
}

// @public
export const createFocusOutlineStyle: ({ enableOutline, selector, style, }?: CreateFocusOutlineStyleOptions) => GriffelStyle;
export const createFocusOutlineStyle: ({ enableOutline, selector, customizeSelector, style, }?: CreateFocusOutlineStyleOptions) => GriffelStyle;

// @public (undocumented)
export interface CreateFocusOutlineStyleOptions extends Omit<CreateCustomFocusIndicatorStyleOptions, 'enableOutline'> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const FOCUS_WITHIN_ATTR = 'data-fui-focus-within';
export const defaultOptions = {
style: {},
selector: 'focus',
customizeSelector: (selector: string) => selector,
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ export interface CreateCustomFocusIndicatorStyleOptions {
* Control if the indicator appears when the corresponding element is focused,
* or any child is focused within the corresponding element.
* @default 'focus'
* @alias selectorType
*/
selector?: 'focus' | 'focus-within';
/**
* Customizes the selector provided based on the selector type.
*/
customizeSelector?: (selector: string) => string;
/**
* Enables the browser default outline style
* @deprecated The custom focus indicator no longer affects outline styles. Outline is overridden
Expand All @@ -30,14 +35,19 @@ export interface CreateCustomFocusIndicatorStyleOptions {
*/
export function createCustomFocusIndicatorStyle<TStyle extends GriffelStyle | GriffelResetStyle>(
style: TStyle,
{ selector = defaultOptions.selector }: CreateCustomFocusIndicatorStyleOptions = defaultOptions,
{
selector: selectorType = defaultOptions.selector,
customizeSelector = defaultOptions.customizeSelector,
}: CreateCustomFocusIndicatorStyleOptions = defaultOptions,
): TStyle extends GriffelStyle ? GriffelStyle : GriffelResetStyle {
return {
...(selector === 'focus' && {
[`&[${FOCUS_VISIBLE_ATTR}]`]: style,
}),
...(selector === 'focus-within' && {
[`&[${FOCUS_WITHIN_ATTR}]:${selector}`]: style,
}),
};
return { [customizeSelector(createBaseSelector(selectorType))]: style };
}

function createBaseSelector(selectorType: 'focus' | 'focus-within'): string {
switch (selectorType) {
case 'focus':
return `&[${FOCUS_VISIBLE_ATTR}]`;
case 'focus-within':
return `&[${FOCUS_WITHIN_ATTR}]:focus-within`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const getFocusOutlineStyles = (options: FocusOutlineStyleOptions): GriffelStyle
export const createFocusOutlineStyle = ({
enableOutline = false,
selector = defaultOptions.selector,
customizeSelector,
style = defaultOptions.style,
}: CreateFocusOutlineStyleOptions = defaultOptions): GriffelStyle => ({
':focus': {
Expand All @@ -105,6 +106,6 @@ export const createFocusOutlineStyle = ({
outlineWidth: '2px',
...style,
}),
{ selector },
{ selector, customizeSelector },
),
});

0 comments on commit e33852b

Please sign in to comment.