Position
position
propertiesimport { position } from 'jynx'
The position
function gives you control of a component's position styles, providing
support for all position-related properties including position
, top
, bottom
, left
,
right
and z-index
.
Usage
To use the position
function, create a component using the PositionProps
type and include the function within it's style argument.
/* Import the function and its corresponding props type */
import { position, PositionProps } from 'jynx'
/* Then define a component with the type and include the
function within its style argument */
export const Component = styled.div<PositionProps>`
${position}
`
/* Or using the legacy functional syntax */
export const Component = styled.div<PositionProps>((props) => `
${position}
`)
Props
By default, when passing a string to any position
props, the raw css value is used. This means you can define position styles however you normally would:
<Component position='relative' />
For top
, bottom
, left
, right
and zIndices
, a numeric value can also be passed.
- when a space scale is defined as an array, numbers between 0 and
space.length
are used as an index - when a space scale is not defined, the original number is used
- negative numbers will return negative values
- in all instances, numeric values are converted to a
px
string where necessary
When using props like position
that support a set of pre-declared string values
(fixed
, sticky
, etc), these will be provided as suggestions in your IDE.
Additionally, any standard named values (inherit
, initial
, etc) are available to use, and when using typescript, will show up as suggestions in your IDE.
Theming
You can also create a space
scale within your theme, which is then accessible from the
top
, bottom
, left
and right
props.
This can be defined as any valid scale type and the values will be shown as suggestions in your IDE.
/* simple array-based scale */
export const theme = {
space: [2, 4, 8, 12, 16, 24, 32, 48, 64, 72]
}
/* simple object-based scale */
export const theme = {
space: {
extraSmall: '0.25rem',
small: '0.5rem',
medium: '1rem',
large: '2rem',
extraLarge: '4rem'
}
}
Types
When working in typescript, use the PositionProps
interface to provide reliable type definitions for your components and value suggestions in your IDE.
interface PositionProps {
position?: StyleProp<CSS.Property.Position>
top?: StyleProp<CSS.Property.Top<string | 0 | number> | ThemeValue<'space'>>
right?: StyleProp<CSS.Property.Right<string | 0 | number> | ThemeValue<'space'>>
bottom?: StyleProp<CSS.Property.Bottom<string | 0 | number> | ThemeValue<'space'>>
left?: StyleProp<CSS.Property.Left<string | 0 | number> | ThemeValue<'space'>>
zIndex?: StyleProp<CSS.Property.ZIndex | ThemeValue<'zIndices'>>
}
API
The position
function allows for a number of props to be passed to your component from directly within your jsx
/ tsx
. Select props also have shorthand aliases that can be used for a more concise syntax.
Prop | Shorthand | CSS Property | Theme Scale |
---|---|---|---|
position | pos | position | |
top | top | space | |
right | right | space | |
bottom | bottom | space | |
left | left | space | |
zIndex | z | z-index | zIndices |
Examples
/* using plain CSS values */
<Component zIndex={2} />
/* using a shorthand alias */
<Component pos='sticky' />
/* with a theme scale defined */
/* `theme.space[4]` */
<Component top={4} />
/* `theme.space.extraLarge` */
<Component left='large' />
/* based on a conditional */
<Component z={isActice ? 2 : 0} />
/* using responsive styles */
<Component pos={['fixed', null, 'sticky']} />
<Component top={{ _: '1rem', md: '1.5rem', lg: '2.5rem' }} />
/* using responsive styles with theme scales */
<Component left={[3, 4, null, 7]} />
<Component right={{_: 3, sm: 4 lg: 7}} />