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

add rtl support for field labels and descriptions #4408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-icons-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tinacms': patch
---

Supports rtl direction in textfield, textarea and all field's labels and descriptions
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import { getDirection } from '../field-utils'

type a = React.DetailedHTMLProps<
React.InputHTMLAttributes<HTMLInputElement>,
Expand All @@ -23,6 +24,7 @@ export const BaseTextField = React.forwardRef<
className={`${textFieldClasses} ${
disabled ? disabledClasses : ''
} ${className}`}
dir={getDirection(rest.value)}
{...rest}
/>
)
Expand Down
2 changes: 2 additions & 0 deletions packages/tinacms/src/toolkit/fields/components/textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import { getDirection } from '../field-utils'

type a = React.DetailedHTMLProps<
React.InputHTMLAttributes<HTMLInputElement>,
Expand All @@ -19,6 +20,7 @@ export const TextArea = React.forwardRef<
className="shadow-inner text-base px-3 py-2 text-gray-600 resize-y focus:shadow-outline focus:border-blue-500 block w-full border border-gray-200 focus:text-gray-900 rounded-md"
ref={ref}
style={{ minHeight: '160px' }}
dir={getDirection(props.value)}
/>
)
})
12 changes: 12 additions & 0 deletions packages/tinacms/src/toolkit/fields/field-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const hasRTLChar = (text: string): boolean => {
return /[\u0600-\u06ef]/g.test(text)
}

export const getDirection = (
value: number | string | boolean | readonly string[]
) => {
if (typeof value === 'string') {
return hasRTLChar(value) ? 'rtl' : 'ltr'
}
return 'unset'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FieldProps } from './field-props'
import { useEvent } from '@toolkit/react-core/use-cms-event'
import { FieldHoverEvent, FieldFocusEvent } from '@toolkit/fields/field-events'
import { Form } from '@toolkit/forms'
import { getDirection } from '../field-utils'

export type InputFieldType<ExtraFieldProps, InputProps> =
FieldProps<InputProps> & ExtraFieldProps
Expand Down Expand Up @@ -82,7 +83,10 @@ export const FieldMeta = ({
onMouseOver={() => setHoveredField({ id: tinaForm.id, fieldName: name })}
onMouseOut={() => setHoveredField({ id: null, fieldName: null })}
onClick={() => setFocusedField({ id: tinaForm.id, fieldName: name })}
style={{ zIndex: index ? 1000 - index : undefined }}
style={{
zIndex: index ? 1000 - index : undefined,
direction: getDirection(label),
}}
{...props}
>
{(label !== false || description) && (
Expand Down