Skip to content

Commit 584700e

Browse files
authored
Merge pull request #550 from framer/feature/code-link-skills
Code Link: Add component best practices skill
2 parents 0b5bd7c + b48033b commit 584700e

File tree

14 files changed

+2789
-12
lines changed

14 files changed

+2789
-12
lines changed

assets/code-link.png

4.99 KB
Loading

packages/code-link-cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"type": "module",
77
"bin": "./dist/index.mjs",
88
"files": [
9-
"dist"
9+
"dist",
10+
"skills"
1011
],
1112
"scripts": {
1213
"dev": "NODE_ENV=development tsx src/index.ts",
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
name: framer-component-best-practices
3+
description: Best practices for building and improving React code components in Framer, a no-code website builder. Covers property controls, animations, accessibility, and platform constraints. Use when creating, editing, or reviewing Framer components, working with ControlType property controls, or building React components for Framer projects.
4+
license: MIT
5+
metadata:
6+
author: framer
7+
version: "1.0"
8+
---
9+
10+
# Framer Component Best Practices
11+
12+
Best practices for building and improving React components in Framer with property controls, animations, and accessibility.
13+
14+
## Core Rules
15+
16+
### Component Structure
17+
18+
```tsx
19+
import { addPropertyControls, ControlType } from "framer";
20+
import { motion } from "framer-motion"; // NOT from "framer"
21+
22+
interface MyComponentProps {
23+
/* typed props */
24+
}
25+
26+
/**
27+
* @framerSupportedLayoutWidth any-prefer-fixed
28+
* @framerSupportedLayoutHeight any-prefer-fixed
29+
*/
30+
export default function MyComponent(props: MyComponentProps) {
31+
// component
32+
}
33+
34+
addPropertyControls(MyComponent, {
35+
/* controls */
36+
});
37+
```
38+
39+
### Platform Constraints
40+
41+
These will cause errors if violated:
42+
43+
1. **Single file, default export** - Use named `function` syntax (not arrow functions), no named exports
44+
2. **Imports** - Only `react`, `react-dom`, `framer`, `framer-motion`. Import `motion` from `"framer-motion"`, not `"framer"`
45+
3. **Position** - Use `position: relative` on the root element, never `fixed`
46+
4. **SSR** - Guard `window`/`document` access: `if (typeof window !== "undefined")`
47+
5. **Annotations** - Include `@framerSupportedLayoutWidth/Height` in a `/** */` block comment immediately above the component function
48+
6. **Types** - Provide a typed props interface (e.g. `MyComponentProps`). Avoid NodeJS types like `Timeout` — use `number` instead
49+
50+
### Layout Annotations
51+
52+
| Content | Width | Height |
53+
| ----------------- | ------------------ | ------------------ |
54+
| No intrinsic size | `fixed` | `fixed` |
55+
| Text/auto-sizing | `auto` | `auto` |
56+
| Flexible | `any-prefer-fixed` | `any-prefer-fixed` |
57+
58+
Detect auto vs fixed sizing: check if `style.width` or `style.height` is `"100%"`.
59+
60+
### Property Controls
61+
62+
To make components configurable in Framer's properties panel, add property controls:
63+
64+
- To make colors customizable, use `ControlType.Color`. Reuse the same prop for elements sharing a color.
65+
- To make text styling customizable, use `ControlType.Font` with `controls: "extended"` and `defaultFontType: "sans-serif"`.
66+
- For images, use `ControlType.ResponsiveImage`. Set defaults in the component body via destructuring (the control doesn't support `defaultValue`).
67+
- Provide a `defaultValue` for every prop so components render correctly in the Framer canvas. Include at least one item in `ControlType.Array` controls.
68+
- `ComponentName.defaultProps` is not supported in Framer — use `defaultValue` on the property control instead.
69+
- Use `hidden` for conditional visibility: `hidden: (props) => !props.showFeature`
70+
- Prefer sliders over steppers unless step values are large.
71+
- Keep controls focused — make key elements configurable, hardcode the rest.
72+
- See [Property Control Guide](references/PROPERTY_CONTROL_GUIDE.md) for detailed patterns, font styling rules, and recommended default values.
73+
74+
### Image Defaults (in component body)
75+
76+
```tsx
77+
const {
78+
image = {
79+
src: "https://framerusercontent.com/images/GfGkADagM4KEibNcIiRUWlfrR0.jpg",
80+
alt: "Default",
81+
},
82+
} = props;
83+
```
84+
85+
### Animation Performance
86+
87+
```tsx
88+
import { useIsStaticRenderer } from "framer";
89+
import { useInView } from "framer-motion";
90+
91+
const isStatic = useIsStaticRenderer();
92+
const ref = useRef(null);
93+
const isInView = useInView(ref);
94+
95+
if (isStatic) return <StaticPreview />; // Show useful static state
96+
// Pause animations when out of viewport
97+
```
98+
99+
- For very complex animations, consider WebGL instead of `framer-motion`.
100+
- Static preview should include visual effects, not just text.
101+
- Wrapping state updates in `startTransition()` prevents UI blocking and keeps interactions smooth.
102+
103+
### Text
104+
105+
- For auto-sized components with text, apply `width: max-content` or `minWidth: max-content` to prevent text from collapsing.
106+
107+
### Common Errors
108+
109+
- WebGL cross-origin: handle `SecurityError: Failed to execute 'texImage2D'` for cross-origin images.
110+
- Inverted Y-axis: check if WebGL images render upside down and accommodate.
111+
112+
### Accessibility
113+
114+
- `aria` roles on interactive elements
115+
- Semantic HTML (`<nav>`, `<article>`, `<section>`)
116+
- `alt=""` on decorative images
117+
- 4.5:1 color contrast
118+
119+
## Term Interpretation
120+
121+
- "responsive" → width/height 100%
122+
- "modern" → 8px radius, 16px spacing, subtle shadows
123+
- "minimal" → limited colors, whitespace
124+
- "interactive" → hover/active states
125+
- "accessible" → ARIA, semantic HTML
126+
- "props"/"properties" → Framer property controls
127+
128+
## Reference Files
129+
130+
- [Property Controls](references/PROPERTY_CONTROLS.md) - All ControlType documentation with examples
131+
- [Property Control Types](references/PROPERTY_CONTROL_TYPES.md) - TypeScript interfaces for all control types
132+
- [Property Control Guide](references/PROPERTY_CONTROL_GUIDE.md) - Font patterns, styling rules, and recommended default values
133+
- [Example Components](references/EXAMPLES.md) - Cookie banner, image compare, sticky notes, twemoji

0 commit comments

Comments
 (0)