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

fix(react-message-bar): force links to be underlined #32850

Merged
merged 13 commits into from
Oct 1, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat: add LinkContextProvider",
"packageName": "@fluentui/react-link",
"email": "vgenaev@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: use LinkContextProvider to force all links inside MessageBody to be underlined",
"packageName": "@fluentui/react-message-bar",
"email": "vgenaev@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export const Link: ForwardRefComponent<LinkProps>;
// @public (undocumented)
export const linkClassNames: SlotClassNames<LinkSlots>;

// @public (undocumented)
export const linkContextDefaultValue: LinkContextValue;

// @public (undocumented)
export const LinkContextProvider: React_2.Provider<LinkContextValue | undefined>;

// @public (undocumented)
export type LinkContextValue = {
inline?: boolean;
};

// @public (undocumented)
export type LinkProps = ComponentProps<LinkSlots> & {
appearance?: 'default' | 'subtle';
Expand All @@ -42,6 +53,9 @@ export const renderLink_unstable: (state: LinkState) => JSX.Element;
// @public
export const useLink_unstable: (props: LinkProps, ref: React_2.Ref<HTMLAnchorElement | HTMLButtonElement | HTMLSpanElement>) => LinkState;

// @public (undocumented)
export const useLinkContext: () => LinkContextValue;

// @public
export const useLinkState_unstable: (state: LinkState) => LinkState;

Expand Down
1 change: 1 addition & 0 deletions packages/react-components/react-link/library/src/Link.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './components/Link/index';
export * from './contexts';
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getIntrinsicElementProps, slot } from '@fluentui/react-utilities';
import { useBackgroundAppearance } from '@fluentui/react-shared-contexts';
import { useLinkState_unstable } from './useLinkState';
import type { LinkProps, LinkState } from './Link.types';
import { useLinkContext } from '../../contexts/linkContext';

/**
* Given user props, defines default props for the Link, calls useLinkState_unstable, and returns processed state.
Expand All @@ -14,6 +15,7 @@ export const useLink_unstable = (
ref: React.Ref<HTMLAnchorElement | HTMLButtonElement | HTMLSpanElement>,
): LinkState => {
const backgroundAppearance = useBackgroundAppearance();
const { inline: inlineContext } = useLinkContext();
const { appearance = 'default', disabled = false, disabledFocusable = false, inline = false } = props;

const elementType = props.as || (props.href ? 'a' : 'button');
Expand All @@ -31,7 +33,7 @@ export const useLink_unstable = (
appearance,
disabled,
disabledFocusable,
inline,
inline: inline ?? !!inlineContext,

// Slots definition
components: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './linkContext';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';

export type LinkContextValue = {
inline?: boolean;
};

const LinkContext = React.createContext<LinkContextValue | undefined>(undefined);

export const linkContextDefaultValue: LinkContextValue = {
inline: false,
};

export const LinkContextProvider = LinkContext.Provider;
export const useLinkContext = () => React.useContext(LinkContext) ?? linkContextDefaultValue;
2 changes: 2 additions & 0 deletions packages/react-components/react-link/library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export {
useLink_unstable,
} from './Link';
export type { LinkProps, LinkSlots, LinkState } from './Link';
export { linkContextDefaultValue, LinkContextProvider, useLinkContext } from './contexts';
export type { LinkContextValue } from './contexts';
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const renderMessageBar_unstable: (state: MessageBarState, contexts: Messa
export const renderMessageBarActions_unstable: (state: MessageBarActionsState, contexts: MessageBarActionsContextValues) => JSX.Element;

// @public
export const renderMessageBarBody_unstable: (state: MessageBarBodyState) => JSX.Element;
export const renderMessageBarBody_unstable: (state: MessageBarBodyState, contextValues: MessageBarBodyContextValues) => JSX.Element;

// @public
export const renderMessageBarGroup_unstable: (state: MessageBarGroupState) => JSX.Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@fluentui/react-icons": "^2.0.245",
"@fluentui/react-jsx-runtime": "^9.0.44",
"@fluentui/react-shared-contexts": "^9.20.1",
"@fluentui/react-link": "^9.2.32",
"@fluentui/react-theme": "^9.1.20",
"@fluentui/react-utilities": "^9.18.15",
"@griffel/react": "^1.5.22",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import { useCustomStyleHook_unstable } from '@fluentui/react-shared-contexts';
import { useMessageBarBody_unstable } from './useMessageBarBody';
import { renderMessageBarBody_unstable } from './renderMessageBarBody';
import { useMessageBarBodyStyles_unstable } from './useMessageBarBodyStyles.styles';
import { useMessageBarBodyContextValues_unstable } from './useMessageBarBodyContextValues';
import type { MessageBarBodyProps } from './MessageBarBody.types';

/**
* MessageBarBody component
*/
export const MessageBarBody: ForwardRefComponent<MessageBarBodyProps> = React.forwardRef((props, ref) => {
const state = useMessageBarBody_unstable(props, ref);
const ctx = useMessageBarBodyContextValues_unstable(state);

useMessageBarBodyStyles_unstable(state);
useCustomStyleHook_unstable('useMessageBarBodyStyles_unstable')(state);
return renderMessageBarBody_unstable(state);
return renderMessageBarBody_unstable(state, ctx);
});

MessageBarBody.displayName = 'MessageBarBody';
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities';

export type MessageBarBodyContextValues = {
link: {
inline?: boolean;
};
};

export type MessageBarBodySlots = {
root: Slot<'div'>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
/** @jsxImportSource @fluentui/react-jsx-runtime */

import { assertSlots } from '@fluentui/react-utilities';
import type { MessageBarBodyState, MessageBarBodySlots } from './MessageBarBody.types';
import type { MessageBarBodyState, MessageBarBodySlots, MessageBarBodyContextValues } from './MessageBarBody.types';
import { LinkContextProvider } from '@fluentui/react-link';

/**
* Render the final JSX of MessageBarBody
*/
export const renderMessageBarBody_unstable = (state: MessageBarBodyState) => {
export const renderMessageBarBody_unstable = (
state: MessageBarBodyState,
contextValues: MessageBarBodyContextValues,
) => {
assertSlots<MessageBarBodySlots>(state);

return <state.root />;
return (
<LinkContextProvider value={contextValues.link}>
<state.root />
</LinkContextProvider>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
import type { MessageBarBodyState, MessageBarBodyContextValues } from './MessageBarBody.types';

export function useMessageBarBodyContextValues_unstable(state: MessageBarBodyState): MessageBarBodyContextValues {
const link = React.useMemo(
() => ({
inline: true,
}),
[],
);

return {
link,
};
}
Loading