0.1.73 • Published 3 days ago

@stackbit/annotations v0.1.73

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

Stackbit Annotations

This package includes several utilities to work with Stackbit annotations in React.

Stackbit annotations enable on-page editing experience for your site. Click here to learn more about Stackbit.

Install

npm install @stackbit/annotations

Usage

import * as React from 'react';
import { toObjectId, toFieldPath } from '@stackbit/annotations';

export function MyComponent(props) {
  return (
    <div {...toObjectId(props.id)}>
       <h1 {...toFieldPath('title')}>{props.title}</h1>
       <img src={props.image} {...toFieldPath('image#@src')} />
       {props.link && (
         <a href={props.link.url} {...toFieldPath('link.url#@href', 'link.text')}>
           {props.link.text}
         </a>
       )}
    </div>
  );
}

Output:

<div data-sb-object-id="...">
   <h1 data-sb-field-path="title">...</h1>
   <img src="..." data-sb-field-path="image#@src" />
   <a href="..." data-sb-field-path="link.url#@href link.text">...</a>
</div>

When NODE_ENV is set to production, toObjectIds and toFieldPath will return empty objects, effectively removing any Stackbit annotations.

Utilities

toObjectId

Takes a string and returns an object with that string inside the data-sb-object-id property. When NODE_ENV is set to production, this function returns an empty object.

toObjectId('xyz');
// result: { 'data-sb-object-id': 'xyz' }

toFieldPath

Takes field-path and returns an object with all field-paths concatenated into the data-sb-field-path property. Each field-path can be a string or a FieldPathDescriptor. When NODE_ENV is set to production, this function returns an empty object.

toFieldPath('title', { objectId: 'xyz', fieldPath: 'url', xpath: '@href' });
// result: { 'data-sb-field-path': 'title xyz:url#@href' }

getObjectId

Takes an object and returns the value of the data-sb-object-id property if it exists, otherwise returns undefined.

getObjectId({ 'data-sb-object-id': 'xyz' });
// result: 'xyz'

getFieldPath

Takes an object and returns the value of the data-sb-field-path property if it exists, otherwise returns undefined.

getFieldPath({ 'data-sb-field-path': 'title' });
// result: 'title'

pickDataAttrs

Takes an object and returns a new object with only the properties that start with data-

getFieldPath({
    foo: 'bar',
    bar: 'foo',
    'data-sb-object-id': 'xyz',
    'data-sb-field-path': 'title'
});
// result:
// {
//   'data-sb-object-id': 'xyz',
//   'data-sb-field-path': 'title'
// }
0.1.73

3 days ago

0.1.73-staging.1

13 days ago

0.1.72

13 days ago

0.1.72-staging.1

20 days ago

0.1.71

20 days ago

0.1.71-staging.1

24 days ago

0.1.70

24 days ago

0.1.70-staging.1

1 month ago

0.1.69

1 month ago

0.1.70-develop.1

1 month ago

0.1.69-staging.1

1 month ago

0.1.68

1 month ago

0.1.68-staging.1

2 months ago

0.1.69-develop.1

2 months ago

0.1.67

2 months ago

0.1.68-develop.1

2 months ago

0.1.67-staging.1

2 months ago

0.1.66

2 months ago

0.1.66-staging.1

2 months ago

0.1.67-develop.1

2 months ago

0.1.65

2 months ago

0.1.66-develop.1

2 months ago

0.1.65-staging.1

2 months ago

0.1.64

2 months ago

0.1.65-develop.1

3 months ago

0.1.64-staging.1

3 months ago

0.1.63

3 months ago

0.1.64-develop.1

3 months ago

0.1.63-staging.1

3 months ago

0.1.62

3 months ago

0.1.62-staging.1

3 months ago

0.1.61

3 months ago

0.1.63-develop.1

3 months ago

0.1.61-staging.1

3 months ago

0.1.62-develop.1

3 months ago

0.1.60

3 months ago

0.1.59

4 months ago

0.1.60-staging.1

4 months ago

0.1.60-develop.1

4 months ago

0.1.61-develop.1

4 months ago

0.1.59-staging.1

4 months ago

0.1.57

4 months ago

0.1.58-staging.1

4 months ago

0.1.59-develop.1

4 months ago

0.1.58-develop.1

4 months ago

0.1.56

4 months ago

0.1.57-staging.1

4 months ago

0.1.55

5 months ago

0.1.56-staging.1

5 months ago

0.1.57-develop.1

5 months ago

0.1.54

5 months ago

0.1.55-staging.1

5 months ago

0.1.56-develop.1

5 months ago

0.1.42-develop.1

7 months ago

0.1.55-develop.1

5 months ago

0.1.50

6 months ago

0.1.51

5 months ago

0.1.49

6 months ago

0.1.42-staging.1

7 months ago

0.1.32-develop.1

10 months ago

0.1.41

7 months ago

0.1.43

7 months ago

0.1.48-staging.1

6 months ago

0.1.44

7 months ago

0.1.47

6 months ago

0.1.38-develop.1

8 months ago

0.1.48

6 months ago

0.1.45-staging.1

7 months ago

0.1.40

7 months ago

0.1.50-develop.1

6 months ago

0.1.35-develop.1

9 months ago

0.1.52-staging.1

5 months ago

0.1.38

8 months ago

0.1.39

8 months ago

0.1.37-staging.1

8 months ago

0.1.50-staging.1

6 months ago

0.1.30

10 months ago

0.1.31

9 months ago

0.1.32

9 months ago

0.1.33

9 months ago

0.1.34

9 months ago

0.1.53-develop.1

5 months ago

0.1.35

9 months ago

0.1.37

8 months ago

0.1.37-develop.1

9 months ago

0.1.30-develop.1

10 months ago

0.1.34-staging.1

9 months ago

0.1.28

10 months ago

0.1.29

10 months ago

0.1.52-develop.1

6 months ago

0.1.51-staging.1

6 months ago

0.1.36-develop.1

9 months ago

0.1.29-develop.1

10 months ago

0.1.35-staging.1

9 months ago

0.1.36-staging.1

9 months ago

0.1.39-develop.1

8 months ago

0.1.51-develop.1

6 months ago

0.1.49-staging.1

6 months ago

0.1.30-staging.2

10 months ago

0.1.29-staging.1

10 months ago

0.1.30-staging.1

10 months ago

0.1.54-develop.1

5 months ago

0.1.49-develop.1

6 months ago

0.1.33-staging.1

9 months ago

0.1.46-develop.1

7 months ago

0.1.41-staging.1

7 months ago

0.1.40-develop.1

8 months ago

0.1.34-develop.1

9 months ago

0.1.31-staging.1

10 months ago

0.1.40-staging.1

8 months ago

0.1.53-staging.1

5 months ago

0.1.38-staging.1

8 months ago

0.1.47-develop.1

7 months ago

0.1.33-develop.1

9 months ago

0.1.32-staging.1

9 months ago

0.1.41-develop.1

8 months ago

0.1.39-staging.1

8 months ago

0.1.46-staging.1

7 months ago

0.1.54-staging.1

5 months ago

0.1.28-staging.1

10 months ago

0.1.27

10 months ago

0.1.26-develop.1

11 months ago

0.1.23-staging.1

11 months ago

0.1.20-develop.1

12 months ago

0.1.23-develop.2

11 months ago

0.1.23-develop.3

11 months ago

0.1.23-develop.1

11 months ago

0.1.22-staging.1

11 months ago

0.1.21-develop.1

12 months ago

0.1.25-staging.1

11 months ago

0.1.24-develop.1

11 months ago

0.1.19-staging.1

12 months ago

0.1.21-staging.1

12 months ago

0.1.20

12 months ago

0.1.21

12 months ago

0.1.22

11 months ago

0.1.25-develop.1

11 months ago

0.1.24-staging.1

11 months ago

0.1.23

11 months ago

0.1.24

11 months ago

0.1.25

10 months ago

0.1.26

10 months ago

0.1.27-staging.1

10 months ago

0.1.22-develop.1

12 months ago

0.1.20-staging.1

12 months ago

0.1.19

12 months ago

0.1.12

1 year ago

0.1.13

1 year ago

0.1.14

1 year ago

0.1.15

1 year ago

0.1.17

1 year ago

0.1.13-gitcms.0

1 year ago

0.1.11

1 year ago

0.1.10-alpha.0

1 year ago

0.1.12-alpha.0

1 year ago

0.1.9-alpha.0

1 year ago

0.1.8

1 year ago

0.1.8-alpha.0

1 year ago

0.1.7-alpha.0

2 years ago

0.1.7

1 year ago

0.1.6

2 years ago

0.1.4-alpha.0

2 years ago

0.1.4

2 years ago

0.1.5

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago