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

[Emotion] Convert EuiFormLabel and EuiFormLegend #7967

Merged
merged 8 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,6 +1,4 @@
.euiFormLegend {
@include euiFormLabel;
Copy link
Member

Choose a reason for hiding this comment

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

I only see this being used once in Kibana, so shouldn't be a big change

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for looking into it! I'll likely change their usage to the <EuiFormLabel /> component directly.


&:not(.euiFormLegend-isHidden) {
margin-bottom: $euiSizeS;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { css } from '@emotion/react';

import { UseEuiTheme } from '../../../services';
import { euiFormLabel } from '../form_label/form_label.styles';

export const euiFormLegendStyles = (euiThemeContext: UseEuiTheme) => {
return {
euiFormLegend: css`
${euiFormLabel(euiThemeContext)}
`,
};
};
12 changes: 10 additions & 2 deletions packages/eui/src/components/form/form_fieldset/form_legend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
*/

import React, { HTMLAttributes, FunctionComponent, ReactNode } from 'react';
import { CommonProps } from '../../common';
import classNames from 'classnames';

import { useEuiMemoizedStyles } from '../../../services';
import { CommonProps } from '../../common';
import { EuiScreenReaderOnly } from '../../accessibility';

import { euiFormLegendStyles } from './form_legend.styles';

export type EuiFormLegendProps = HTMLAttributes<HTMLLegendElement> &
CommonProps & {
/**
Expand All @@ -32,6 +36,10 @@ export const EuiFormLegend: FunctionComponent<EuiFormLegendProps> = ({
...rest
}) => {
const isLegendHidden = display === 'hidden';

const styles = useEuiMemoizedStyles(euiFormLegendStyles);
const cssStyles = [styles.euiFormLegend];

const classes = classNames(
'euiFormLegend',
{
Expand All @@ -42,7 +50,7 @@ export const EuiFormLegend: FunctionComponent<EuiFormLegendProps> = ({
);

return (
<legend className={classes} {...rest}>
<legend css={cssStyles} className={classes} {...rest}>
{isLegendHidden ? (
<EuiScreenReaderOnly>
<span>{children}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* 2. Disabled state overrides pointer.
*/
.euiFormLabel {
Copy link
Member

Choose a reason for hiding this comment

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

When we remove this from our sass files ... is it still available globally for use like this? Or will that cause an issue: https://github.com/elastic/kibana/blob/main/x-pack/plugins/cases/public/components/configure_cases/field_mapping.tsx#L35

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, it will cause an issue! I'll change their usage to the EuiFormLabel component directly

@include euiFormLabel;
display: inline-block;
transition: all $euiAnimSpeedFast $euiAnimSlightResistance;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { css } from '@emotion/react';
import { serializeStyles, type CSSObject } from '@emotion/serialize';

import { UseEuiTheme } from '../../../services';
import { euiTextBreakWord } from '../../../global_styling';
import { euiTitle } from '../../title/title.styles';

export const euiFormLabel = (euiThemeContext: UseEuiTheme) => {
const { euiTheme } = euiThemeContext;
// Exclude the fontWeight from the title, since we're setting our own later
const { fontWeight: _, ..._titleStyles } = euiTitle(euiThemeContext, 'xxxs');
// Since we're not returning a css`` string (to avoid generating an extra Emotion
// className), we need to manually serialize the style object into a string
const titleStyles = serializeStyles([_titleStyles as CSSObject]).styles;

return `
${titleStyles}
font-weight: ${euiTheme.font.weight.semiBold};
${euiTextBreakWord()}
`;
};

export const euiFormLabelStyles = (euiThemeContext: UseEuiTheme) => {
return {
euiFormLabel: css`
${euiFormLabel(euiThemeContext)}
`,
};
};
9 changes: 9 additions & 0 deletions packages/eui/src/components/form/form_label/form_label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import React, {
HTMLAttributes,
} from 'react';
import classNames from 'classnames';

import { useEuiMemoizedStyles } from '../../../services';
import { CommonProps, ExclusiveUnion } from '../../common';

import { euiFormLabelStyles } from './form_label.styles';

interface EuiFormLabelCommonProps {
isFocused?: boolean;
isInvalid?: boolean;
Expand Down Expand Up @@ -54,6 +58,9 @@ export const EuiFormLabel: FunctionComponent<EuiFormLabelProps> = ({
className,
...rest
}: EuiFormLabelProps) => {
const styles = useEuiMemoizedStyles(euiFormLabelStyles);
const cssStyles = [styles.euiFormLabel];

const classes = classNames('euiFormLabel', className, {
'euiFormLabel-isFocused': isFocused,
'euiFormLabel-isInvalid': isInvalid,
Expand All @@ -63,6 +70,7 @@ export const EuiFormLabel: FunctionComponent<EuiFormLabelProps> = ({
if (type === 'legend') {
return (
<legend
css={cssStyles}
className={classes}
{...(rest as HTMLAttributes<HTMLLegendElement>)}
>
Expand All @@ -72,6 +80,7 @@ export const EuiFormLabel: FunctionComponent<EuiFormLabelProps> = ({
} else {
return (
<label
css={cssStyles}
className={classes}
{...(rest as LabelHTMLAttributes<HTMLLabelElement>)}
>
Expand Down
8 changes: 0 additions & 8 deletions packages/eui/src/global_styling/mixins/_form.scss
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// Labels
@mixin euiFormLabel {
@include euiFontSizeXS;
@include euiTextBreakWord;
color: $euiTitleColor;
font-weight: $euiFontWeightSemiBold;
}

@mixin euiFormControlLayoutPadding($numOfIcons, $side: 'right', $compressed: false) {
$iconSize: $euiSize;
$iconPadding: $euiFormControlPadding;
Expand Down