|
| 1 | +# @softwarity/interactive-code |
| 2 | + |
| 3 | +A Web Component for displaying syntax-highlighted code with interactive bindings. Perfect for documentation, tutorials, and live demos. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Syntax Highlighting**: HTML, SCSS, TypeScript, and Shell |
| 8 | +- **Interactive Bindings**: Click to edit values directly in the code |
| 9 | +- **Multiple Types**: boolean, number, string, select, color, comment |
| 10 | +- **Framework Agnostic**: Works with Angular, React, Vue, or vanilla JS |
| 11 | +- **Zero Dependencies**: Pure Web Components |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install @softwarity/interactive-code |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```typescript |
| 22 | +import '@softwarity/interactive-code'; |
| 23 | +``` |
| 24 | + |
| 25 | +```html |
| 26 | +<interactive-code language="scss"> |
| 27 | + <textarea> |
| 28 | +:root { |
| 29 | + --width: ${width}px; |
| 30 | + --enabled: ${enabled}; |
| 31 | +} |
| 32 | + </textarea> |
| 33 | + <code-binding key="width" type="number" value="72" min="48" max="120"></code-binding> |
| 34 | + <code-binding key="enabled" type="boolean" value="true"></code-binding> |
| 35 | +</interactive-code> |
| 36 | +``` |
| 37 | + |
| 38 | +## Binding Types |
| 39 | + |
| 40 | +| Type | Description | Interaction | |
| 41 | +|------|-------------|-------------| |
| 42 | +| `boolean` | true/false value | Click to toggle | |
| 43 | +| `number` | Numeric value | Click to edit, supports min/max/step | |
| 44 | +| `string` | Text value | Click to edit | |
| 45 | +| `select` | Option from list | Click to toggle (2 options) or dropdown (3+) | |
| 46 | +| `color` | Color value | Click to open color picker | |
| 47 | +| `comment` | Line toggle | Checkbox to comment/uncomment line | |
| 48 | +| `readonly` | Display only | No interaction | |
| 49 | + |
| 50 | +## API |
| 51 | + |
| 52 | +### `<interactive-code>` |
| 53 | + |
| 54 | +| Attribute | Type | Description | |
| 55 | +|-----------|------|-------------| |
| 56 | +| `language` | `'html' \| 'scss' \| 'typescript' \| 'shell'` | Syntax highlighting language | |
| 57 | + |
| 58 | +| Property | Type | Description | |
| 59 | +|----------|------|-------------| |
| 60 | +| `code` | `string \| null` | Set code content programmatically | |
| 61 | + |
| 62 | +### `<code-binding>` |
| 63 | + |
| 64 | +| Attribute | Type | Description | |
| 65 | +|-----------|------|-------------| |
| 66 | +| `key` | `string` | Binding identifier (matches `${key}` in template) | |
| 67 | +| `type` | `BindingType` | Type of binding | |
| 68 | +| `value` | `any` | Initial value | |
| 69 | +| `disabled` | `boolean` | Disable editing | |
| 70 | +| `min` | `number` | Minimum value (for `number` type) | |
| 71 | +| `max` | `number` | Maximum value (for `number` type) | |
| 72 | +| `step` | `number` | Step increment (for `number` type) | |
| 73 | +| `options` | `string` | Comma-separated options (for `select` type) | |
| 74 | + |
| 75 | +| Event | Description | |
| 76 | +|-------|-------------| |
| 77 | +| `change` | Fired when value changes | |
| 78 | + |
| 79 | +**Inline handler:** Use `onchange` attribute where `e` is the new value directly: |
| 80 | + |
| 81 | +```html |
| 82 | +<code-binding key="checked" type="boolean" value="true" |
| 83 | + onchange="document.getElementById('preview').checked = e"></code-binding> |
| 84 | +``` |
| 85 | + |
| 86 | +**addEventListener:** Use `e.target.value` or `e.detail`: |
| 87 | + |
| 88 | +```javascript |
| 89 | +binding.addEventListener('change', (e) => { |
| 90 | + preview.checked = e.target.value; |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +## Examples |
| 95 | + |
| 96 | +### Boolean Toggle |
| 97 | + |
| 98 | +```html |
| 99 | +<interactive-code language="html"> |
| 100 | + <textarea> |
| 101 | +<nav [autoCollapse]="${autoCollapse}">...</nav> |
| 102 | + </textarea> |
| 103 | + <code-binding key="autoCollapse" type="boolean" value="true"></code-binding> |
| 104 | +</interactive-code> |
| 105 | +``` |
| 106 | + |
| 107 | +### Number with Constraints |
| 108 | + |
| 109 | +```html |
| 110 | +<interactive-code language="scss"> |
| 111 | + <textarea> |
| 112 | +:root { |
| 113 | + --width: ${width}px; |
| 114 | +} |
| 115 | + </textarea> |
| 116 | + <code-binding key="width" type="number" value="72" min="48" max="120" step="4"></code-binding> |
| 117 | +</interactive-code> |
| 118 | +``` |
| 119 | + |
| 120 | +### Color Picker |
| 121 | + |
| 122 | +```html |
| 123 | +<interactive-code language="scss"> |
| 124 | + <textarea> |
| 125 | +:root { |
| 126 | + --primary: ${primary}; |
| 127 | +} |
| 128 | + </textarea> |
| 129 | + <code-binding key="primary" type="color" value="#6750a4"></code-binding> |
| 130 | +</interactive-code> |
| 131 | +``` |
| 132 | + |
| 133 | +### Comment Toggle (Line Enable/Disable) |
| 134 | + |
| 135 | +```html |
| 136 | +<interactive-code language="scss"> |
| 137 | + <textarea> |
| 138 | +:root { |
| 139 | + ${enableWidth}--custom-width: 280px; |
| 140 | +} |
| 141 | + </textarea> |
| 142 | + <code-binding key="enableWidth" type="comment" value="true"></code-binding> |
| 143 | +</interactive-code> |
| 144 | +``` |
| 145 | + |
| 146 | +## CSS Customization |
| 147 | + |
| 148 | +The component supports CSS custom properties for styling: |
| 149 | + |
| 150 | +| Property | Default | Description | |
| 151 | +|----------|---------|-------------| |
| 152 | +| `--code-bg` | `#1e1e1e` | Background color of the code block | |
| 153 | +| `--code-border-radius` | `8px` | Border radius of the code block | |
| 154 | + |
| 155 | +```css |
| 156 | +interactive-code { |
| 157 | + --code-bg: #282c34; |
| 158 | + --code-border-radius: 4px; |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Development |
| 163 | + |
| 164 | +```bash |
| 165 | +# Install dependencies |
| 166 | +npm install |
| 167 | + |
| 168 | +# Start dev server |
| 169 | +npm run dev |
| 170 | + |
| 171 | +# Build for production |
| 172 | +npm run build |
| 173 | +``` |
| 174 | + |
| 175 | +## License |
| 176 | + |
| 177 | +MIT - [Softwarity](https://www.softwarity.io/) |
0 commit comments